Skip to content

Commit

Permalink
🦄 refactor: Replace JavetSanitizer with Swc4j
Browse files Browse the repository at this point in the history
  • Loading branch information
caoccao committed Jun 2, 2024
1 parent 34db346 commit cd7cec4
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 28 deletions.
10 changes: 5 additions & 5 deletions console/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ object Config {
const val JAVET = "com.caoccao.javet:javet:${Versions.JAVET}"
const val JAVET_LINUX_ARM64 = "com.caoccao.javet:javet-linux-arm64:${Versions.JAVET}"
const val JAVET_MACOS = "com.caoccao.javet:javet-macos:${Versions.JAVET}"
const val JAVET_SANITIZER = "com.caoccao.javet.sanitizer:javet-sanitizer:${Versions.JAVET_SANITIZER}"
const val JAVET_SWC4J = "com.caoccao.javet:swc4j:${Versions.JAVET_SWC4J}"

// https://mvnrepository.com/artifact/org.eclipse.jetty.websocket/javax-websocket-server-impl
const val JETTY_JAVAX_WEBSOCKET_SERVER_IMPL =
Expand Down Expand Up @@ -62,10 +62,10 @@ object Config {
object Versions {
const val ANTLR4 = "4.13.1"
const val BYTE_BUDDY = "1.14.10"
const val JAVENODE = "0.6.0"
const val JAVET = "3.1.0"
const val JAVET_SANITIZER = "0.2.0-antlr-4.13.1"
const val JAVENODE = "0.7.0"
const val JAVET = "3.1.2"
const val JAVET_SHELL = "0.1.0"
const val JAVET_SWC4J = "0.8.0"
const val JETTY_WEBSOCKET = "9.4.53.v20231009"
const val JUNIT = "5.10.1"
const val KOTLIN_STDLIB_JDK8 = "1.9.21"
Expand Down Expand Up @@ -100,7 +100,7 @@ dependencies {
} else {
implementation(Config.Projects.JAVET)
}
implementation(Config.Projects.JAVET_SANITIZER)
implementation(Config.Projects.JAVET_SWC4J)
implementation(Config.Projects.JETTY_JAVAX_WEBSOCKET_SERVER_IMPL)
implementation(Config.Projects.JETTY_WEBSOCKET_SERVER)
implementation(Config.Projects.KOTLIN_STDLIB_JDK8)
Expand Down
38 changes: 17 additions & 21 deletions console/src/main/kotlin/com/caoccao/javet/shell/BaseJavetShell.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,28 @@ import com.caoccao.javet.exceptions.JavetExecutionException
import com.caoccao.javet.interfaces.IJavetLogger
import com.caoccao.javet.interop.V8Host
import com.caoccao.javet.interop.V8Runtime
import com.caoccao.javet.sanitizer.antlr.JavaScriptParser
import com.caoccao.javet.sanitizer.exceptions.JavetSanitizerException
import com.caoccao.javet.sanitizer.parsers.JavaScriptStatementListParser
import com.caoccao.javet.sanitizer.utils.StringUtils
import com.caoccao.javet.shell.constants.Constants
import com.caoccao.javet.shell.entities.Options
import com.caoccao.javet.shell.enums.ExitCode
import com.caoccao.javet.shell.utils.JavetShellDefaultLogger
import com.caoccao.javet.shell.utils.JavetShellSilentLogger
import com.caoccao.javet.swc4j.Swc4j
import com.caoccao.javet.swc4j.enums.Swc4jMediaType
import com.caoccao.javet.swc4j.enums.Swc4jParseMode
import com.caoccao.javet.swc4j.options.Swc4jParseOptions
import com.caoccao.javet.utils.StringUtils
import com.caoccao.javet.values.V8Value
import sun.misc.Signal
import java.io.File
import java.util.*

abstract class BaseJavetShell(protected val options: Options) {
protected val logger = JavetShellDefaultLogger()
protected val swc4j = Swc4j()
protected val parseOptions = Swc4jParseOptions()
.setParseMode(Swc4jParseMode.Program)
.setMediaType(Swc4jMediaType.JavaScript)
protected abstract val prompt: String

protected abstract fun createEventLoop(
Expand All @@ -63,7 +69,6 @@ abstract class BaseJavetShell(protected val options: Options) {
registerPromiseRejectCallback(v8Runtime)
Scanner(System.`in`).use { scanner ->
val sb = StringBuilder()
var isESM = false
var isMultiline = false
var isBlockCompleted = false
while (eventLoop.running) {
Expand All @@ -84,25 +89,19 @@ abstract class BaseJavetShell(protected val options: Options) {
if (eventLoop.running) {
val codeString = sb.toString()
if (codeString.isNotEmpty()) {
if (!isESM) {
val parser = if (isBlockCompleted) {
try {
JavaScriptStatementListParser(codeString).parse()
} catch (e: JavetSanitizerException) {
null
}
} else {
JavaScriptStatementListParser(codeString).parse()
}
val context = parser?.javaScriptStatementParsers?.first()?.context
if (context != null && context.childCount > 0) {
isESM = context.getChild(0) is JavaScriptParser.ImportStatementContext
val isESM = if (isBlockCompleted) {
try {
swc4j.parse(codeString, parseOptions).parseMode == Swc4jParseMode.Module
} catch (ignored: Throwable) {
false
}
} else {
false
}
v8Runtime
.getExecutor(codeString)
.setResourceName(File(options.scriptName).absolutePath)
.setModule(isESM && isBlockCompleted)
.setModule(isESM)
.execute<V8Value>()
.use { v8Value ->
logger.info(v8Value.toString())
Expand Down Expand Up @@ -137,9 +136,6 @@ abstract class BaseJavetShell(protected val options: Options) {
if (!isMultiline) {
sb.clear()
}
if (isBlockCompleted) {
isESM = false
}
isBlockCompleted = false
}
}
Expand Down
4 changes: 2 additions & 2 deletions docker/console.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ RUN gradle build

# Build Cache
WORKDIR /console/build/libs
RUN unzip ./javet-shell-${VERSION}.jar '*.so'
RUN zip -d ./javet-shell-${VERSION}.jar *.dll *.dylib *.so
RUN unzip ./javet-shell-${VERSION}.jar 'libjavet-*.so'
RUN zip -d ./javet-shell-${VERSION}.jar *.dll *.dylib libjavet-*.so

# Build Shell Scripts
WORKDIR /console/build/libs
Expand Down

0 comments on commit cd7cec4

Please sign in to comment.