Skip to content

Commit

Permalink
✨ feat: Add --verbose
Browse files Browse the repository at this point in the history
  • Loading branch information
caoccao committed Dec 29, 2023
1 parent a8803f1 commit acd1e56
Show file tree
Hide file tree
Showing 15 changed files with 123 additions and 68 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Options:
--debugPort, -p [9229] -> Debug port { Int }
--runtimeType, -r [V8] -> JS runtime type { Value should be one of [node, v8] }
--scriptName, -s [main.js] -> Script name { String }
--verbose, -v [false] -> Verbose
--help, -h -> Usage info
```
Expand Down
10 changes: 5 additions & 5 deletions console/src/main/kotlin/com/caoccao/javet/shell/BaseEventLoop.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package com.caoccao.javet.shell

import com.caoccao.javet.enums.V8AwaitMode
import com.caoccao.javet.interfaces.IJavetClosable
import com.caoccao.javet.interfaces.IJavetLogger
import com.caoccao.javet.interop.V8Runtime
import com.caoccao.javet.javenode.JNEventLoop
import com.caoccao.javet.shell.constants.Constants
Expand All @@ -35,6 +36,7 @@ import java.util.concurrent.TimeUnit
import javax.servlet.ServletContext

abstract class BaseEventLoop(
protected val logger: IJavetLogger,
protected val v8Runtime: V8Runtime,
protected val options: Options,
) : IJavetClosable, Runnable {
Expand Down Expand Up @@ -93,20 +95,18 @@ abstract class BaseEventLoop(
inspectorServer!!.setHandler(inspectorServletContextHandler)
val servletHolder = ServletHolder(InspectorHttpServlet(options))
inspectorServletContextHandler.addServlet(servletHolder, Constants.Inspector.PATH_ROOT)
inspectorServletContextHandler.addServlet(servletHolder, Constants.Inspector.PATH_JSON)
inspectorServletContextHandler.addServlet(servletHolder, Constants.Inspector.PATH_JSON_VERSION)
NativeWebSocketServletContainerInitializer.configure(inspectorServletContextHandler)
{ _: ServletContext, nativeWebSocketConfiguration: NativeWebSocketConfiguration ->
nativeWebSocketConfiguration.policy.maxTextMessageBufferSize = 0xFFFFFF
nativeWebSocketConfiguration.addMapping(
Constants.Inspector.PATH_JAVET,
InspectorWebSocketCreator(v8Runtime, options),
InspectorWebSocketCreator(logger, v8Runtime, options),
)
}
WebSocketUpgradeFilter.configure(inspectorServletContextHandler)
inspectorServer!!.start()
} catch (t: Throwable) {
println("\nError: ${t.message}\n")
logger.error("\nError: ${t.message}\n")
}
}

Expand All @@ -117,7 +117,7 @@ abstract class BaseEventLoop(
try {
inspectorServer!!.stop()
} catch (t: Throwable) {
println("\nError: ${t.message}\n")
logger.error("\nError: ${t.message}\n")
}
}
daemonThread?.join()
Expand Down
52 changes: 25 additions & 27 deletions console/src/main/kotlin/com/caoccao/javet/shell/BaseJavetShell.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ package com.caoccao.javet.shell

import com.caoccao.javet.exceptions.JavetCompilationException
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
Expand All @@ -33,23 +35,27 @@ import sun.misc.Signal
import java.io.File
import java.util.*

abstract class BaseJavetShell(
protected val options: Options,
) {
abstract class BaseJavetShell(protected val options: Options) {
protected val logger = JavetShellDefaultLogger()
protected abstract val prompt: String

protected abstract fun createEventLoop(v8Runtime: V8Runtime, options: Options): BaseEventLoop
protected abstract fun createEventLoop(
logger: IJavetLogger,
v8Runtime: V8Runtime,
options: Options,
): BaseEventLoop

fun execute(): ExitCode {
println("${Constants.Application.NAME} v${Constants.Application.VERSION} (${options.jsRuntimeType.name} ${options.jsRuntimeType.version})")
println(Constants.Application.PROMPT_STRING)
println("Debug port is ${options.debugPort}")
println()
logger.info("${Constants.Application.NAME} v${Constants.Application.VERSION} (${options.jsRuntimeType.name} ${options.jsRuntimeType.version})")
logger.info(Constants.Application.PROMPT_STRING)
logger.info("Debug port is ${options.debugPort}")
logger.info(StringUtils.EMPTY)
val customLogger = if (options.verbose) logger else JavetShellSilentLogger()
V8Host.getInstance(options.jsRuntimeType).createV8Runtime<V8Runtime>().use { v8Runtime ->
v8Runtime.logger = JavetShellDefaultLogger()
v8Runtime.v8Inspector.logger = JavetShellSilentLogger()
v8Runtime.logger = customLogger
v8Runtime.v8Inspector.logger = customLogger
v8Runtime.converter = Constants.Javet.JAVET_PROXY_CONVERTER
createEventLoop(v8Runtime, options).use { eventLoop ->
createEventLoop(customLogger, v8Runtime, options).use { eventLoop ->
Signal.handle(Signal("INT")) {
// Stop the event loop after Ctrl+C is pressed.
eventLoop.running = false
Expand All @@ -66,7 +72,7 @@ abstract class BaseJavetShell(
try {
val line = scanner.nextLine()
if (line == null) {
println()
logger.info(StringUtils.EMPTY)
eventLoop.running = false
break
} else if (line.isBlank()) {
Expand Down Expand Up @@ -99,42 +105,34 @@ abstract class BaseJavetShell(
.setModule(isESM && isBlockCompleted)
.execute<V8Value>()
.use { v8Value ->
println(v8Value.toString())
logger.info(v8Value.toString())
}
}
isMultiline = false
} else {
println()
logger.info(StringUtils.EMPTY)
break
}
} catch (e: JavetSanitizerException) {
if (isBlockCompleted) {
println()
println(e.message)
println()
logger.error("\n${e.message}\n")
}
isMultiline = !isBlockCompleted
} catch (e: JavetCompilationException) {
if (isBlockCompleted) {
println()
println(e.scriptingError.toString())
println()
logger.error("\n${e.scriptingError}\n")
}
isMultiline = !isBlockCompleted
} catch (e: JavetExecutionException) {
isMultiline = false
println()
println(e.scriptingError.toString())
println()
logger.error("\n${e.scriptingError}\n")
} catch (e: NoSuchElementException) {
println()
logger.info(StringUtils.EMPTY)
eventLoop.running = false
break
} catch (t: Throwable) {
isMultiline = false
println()
println(t.message)
println()
logger.error("\n${t.message}\n")
} finally {
if (!isMultiline) {
sb.clear()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,16 @@

package com.caoccao.javet.shell

import com.caoccao.javet.interfaces.IJavetLogger
import com.caoccao.javet.interop.V8Runtime
import com.caoccao.javet.shell.entities.Options
import com.caoccao.javet.shell.enums.JavetShellModuleType

class EventLoopNode(
logger: IJavetLogger,
v8Runtime: V8Runtime,
options: Options,
) : BaseEventLoop(v8Runtime, options) {
) : BaseEventLoop(logger, v8Runtime, options) {
override fun start() {
super.start()
jnEventLoop?.loadStaticModules(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,18 @@

package com.caoccao.javet.shell

import com.caoccao.javet.interfaces.IJavetLogger
import com.caoccao.javet.interop.V8Runtime
import com.caoccao.javet.javenode.JNEventLoop
import com.caoccao.javet.javenode.enums.JNModuleType
import com.caoccao.javet.shell.entities.Options
import com.caoccao.javet.shell.enums.JavetShellModuleType

class EventLoopV8(
logger: IJavetLogger,
v8Runtime: V8Runtime,
options: Options,
) : BaseEventLoop(v8Runtime, options) {
) : BaseEventLoop(logger, v8Runtime, options) {
override fun start() {
super.start()
jnEventLoop?.loadStaticModules(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.caoccao.javet.shell

import com.caoccao.javet.interfaces.IJavetLogger
import com.caoccao.javet.interop.V8Runtime
import com.caoccao.javet.interop.callback.JavetBuiltInModuleResolver
import com.caoccao.javet.shell.entities.Options
Expand All @@ -39,8 +40,12 @@ class JavetShellNode(
override val prompt: String
get() = "N > "

override fun createEventLoop(v8Runtime: V8Runtime, options: Options): BaseEventLoop {
return EventLoopNode(v8Runtime, options)
override fun createEventLoop(
logger: IJavetLogger,
v8Runtime: V8Runtime,
options: Options,
): BaseEventLoop {
return EventLoopNode(logger, v8Runtime, options)
}

override fun registerPromiseRejectCallback(v8Runtime: V8Runtime) {
Expand Down
13 changes: 8 additions & 5 deletions console/src/main/kotlin/com/caoccao/javet/shell/JavetShellV8.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.caoccao.javet.shell

import com.caoccao.javet.interfaces.IJavetLogger
import com.caoccao.javet.interop.V8Runtime
import com.caoccao.javet.shell.entities.Options

Expand All @@ -29,15 +30,17 @@ class JavetShellV8(
override val prompt: String
get() = "V > "

override fun createEventLoop(v8Runtime: V8Runtime, options: Options): BaseEventLoop {
return EventLoopV8(v8Runtime, options)
override fun createEventLoop(
logger: IJavetLogger,
v8Runtime: V8Runtime,
options: Options,
): BaseEventLoop {
return EventLoopV8(logger, v8Runtime, options)
}

override fun registerPromiseRejectCallback(v8Runtime: V8Runtime) {
v8Runtime.setPromiseRejectCallback { _, _, value ->
println()
println(value.toString())
println()
logger.warn("\n${value}\n")
}
}
}
6 changes: 6 additions & 0 deletions console/src/main/kotlin/com/caoccao/javet/shell/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,17 @@ fun main(args: Array<String>) {
shortName = Constants.Options.SCRIPT_NAME_SHORT_NAME,
description = Constants.Options.SCRIPT_NAME_DESCRIPTION,
).default(Constants.Options.SCRIPT_NAME_DEFAULT_VALUE)
val verbose by argParser.option(
ArgType.Boolean,
shortName = Constants.Options.VERBOSE_SHORT_NAME,
description = Constants.Options.VERBOSE_DESCRIPTION,
).default(Constants.Options.VERBOSE_DEFAULT_VALUE)
argParser.parse(args)
val options = Options(
debugPort.toUInt(),
runtimeType.value,
scriptName,
verbose,
)
ByteArrayOutputStream().use { byteArrayOutputStream ->
PrintStream(byteArrayOutputStream).use { printStream ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ object Constants {
const val SCRIPT_NAME_DEFAULT_VALUE = "main.js"
const val SCRIPT_NAME_DESCRIPTION = "Script name"
const val SCRIPT_NAME_SHORT_NAME = "s"
const val VERBOSE_DEFAULT_VALUE = false
const val VERBOSE_DESCRIPTION = "Verbose"
const val VERBOSE_SHORT_NAME = "v"
}

object Inspector {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ data class Options(
val debugPort: UInt,
val jsRuntimeType: JSRuntimeType,
val scriptName: String,
val verbose: Boolean,
)
Original file line number Diff line number Diff line change
Expand Up @@ -16,33 +16,38 @@

package com.caoccao.javet.shell.inspector

import com.caoccao.javet.interfaces.IJavetLogger
import com.caoccao.javet.interop.IV8InspectorListener
import com.caoccao.javet.interop.V8Runtime
import com.caoccao.javet.shell.constants.Constants
import com.caoccao.javet.shell.entities.Options
import org.eclipse.jetty.websocket.api.Session
import org.eclipse.jetty.websocket.api.WebSocketAdapter

class InspectorWebSocketAdapter(val v8Runtime: V8Runtime, val options: Options) : WebSocketAdapter(),
class InspectorWebSocketAdapter(
val logger: IJavetLogger,
val v8Runtime: V8Runtime,
val options: Options,
) : WebSocketAdapter(),
IV8InspectorListener {
override fun flushProtocolNotifications() {
}

override fun onWebSocketClose(statusCode: Int, reason: String?) {
v8Runtime.v8Inspector.removeListeners(this)
println("\nDebug server is closed.\n")
logger.debug("\nDebug server is closed.\n")
super.onWebSocketClose(statusCode, reason)
}

override fun onWebSocketConnect(session: Session?) {
super.onWebSocketConnect(session)
println("\nDebug server is open at ws://${Constants.Inspector.getWebSocketUrl(options.debugPort)}.\n")
logger.debug("\nDebug server is open at ws://${Constants.Inspector.getWebSocketUrl(options.debugPort)}.\n")
v8Runtime.v8Inspector.addListeners(this)
}

override fun onWebSocketError(cause: Throwable?) {
cause?.let { t ->
println("\nError: ${t.message}\n")
logger.error("\nError: ${t.message}\n")
}
}

Expand All @@ -51,7 +56,7 @@ class InspectorWebSocketAdapter(val v8Runtime: V8Runtime, val options: Options)
try {
v8Runtime.v8Inspector.sendRequest(message);
} catch (t: Throwable) {
println("\nError: ${t.message}\n")
logger.error("\nError: ${t.message}\n")
}
}
}
Expand All @@ -61,7 +66,7 @@ class InspectorWebSocketAdapter(val v8Runtime: V8Runtime, val options: Options)
try {
remote.sendString(message);
} catch (t: Throwable) {
println("\nError: ${t.message}\n")
logger.error("\nError: ${t.message}\n")
}
}
}
Expand All @@ -71,7 +76,7 @@ class InspectorWebSocketAdapter(val v8Runtime: V8Runtime, val options: Options)
try {
remote.sendString(message);
} catch (t: Throwable) {
println("\nError: ${t.message}\n")
logger.error("\nError: ${t.message}\n")
}
}
}
Expand All @@ -80,7 +85,7 @@ class InspectorWebSocketAdapter(val v8Runtime: V8Runtime, val options: Options)
try {
v8Runtime.getExecutor("console.log('Welcome to Javet Debugging Environment!');").executeVoid();
} catch (t: Throwable) {
println("\nError: ${t.message}\n")
logger.error("\nError: ${t.message}\n")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,20 @@

package com.caoccao.javet.shell.inspector

import com.caoccao.javet.interfaces.IJavetLogger
import com.caoccao.javet.interop.V8Runtime
import com.caoccao.javet.shell.entities.Options
import org.eclipse.jetty.websocket.servlet.ServletUpgradeRequest
import org.eclipse.jetty.websocket.servlet.ServletUpgradeResponse
import org.eclipse.jetty.websocket.servlet.WebSocketCreator

class InspectorWebSocketCreator(val v8Runtime: V8Runtime, val options: Options) : WebSocketCreator {
class InspectorWebSocketCreator(
val logger: IJavetLogger,
val v8Runtime: V8Runtime,
val options: Options,
) : WebSocketCreator {
override fun createWebSocket(
request: ServletUpgradeRequest,
response: ServletUpgradeResponse,
) = InspectorWebSocketAdapter(v8Runtime, options)
) = InspectorWebSocketAdapter(logger, v8Runtime, options)
}
Loading

0 comments on commit acd1e56

Please sign in to comment.