Skip to content

Commit

Permalink
🦄 refactor: Add 2 const val strings
Browse files Browse the repository at this point in the history
  • Loading branch information
caoccao committed Nov 29, 2023
1 parent d21e6d6 commit e3feb8f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,17 @@ import java.util.*
abstract class BaseJavetShell(
protected val options: Options,
) {
companion object {
const val PROMPT_STRING = "Please input the script or press Ctrl+C to exit."
}

protected abstract val prompt: String

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

fun execute(): ExitCode {
println("${Constants.Application.NAME} v${Constants.Application.VERSION} (${options.jsRuntimeType.name} ${options.jsRuntimeType.version})")
println("Please input the script or press Ctrl+C to exit.")
println(PROMPT_STRING)
println()
V8Host.getInstance(options.jsRuntimeType).createV8Runtime<V8Runtime>().use { v8Runtime ->
v8Runtime.logger = JavetShellLogger()
Expand Down
18 changes: 10 additions & 8 deletions console/src/main/kotlin/com/caoccao/javet/shell/JavetShellNode.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ import com.caoccao.javet.shell.entities.Options
class JavetShellNode(
options: Options,
) : BaseJavetShell(options) {
companion object {
const val PROMISE_REJECT_CALLBACK_CODE_STRING = """const process = require('process');
process.on('unhandledRejection', (reason, promise) => {
console.error();
console.error(reason.toString());
console.error();
});"""
}

init {
assert(options.jsRuntimeType.isNode) { "JS runtime type must be Node." }
}
Expand All @@ -36,13 +45,6 @@ class JavetShellNode(

override fun registerPromiseRejectCallback(v8Runtime: V8Runtime) {
v8Runtime.v8ModuleResolver = JavetBuiltInModuleResolver()
v8Runtime.getExecutor(
"""const process = require('process');
process.on('unhandledRejection', (reason, promise) => {
console.error();
console.error(reason.toString());
console.error();
});"""
)?.executeVoid()
v8Runtime.getExecutor(PROMISE_REJECT_CALLBACK_CODE_STRING).executeVoid()
}
}

0 comments on commit e3feb8f

Please sign in to comment.