Skip to content

Commit

Permalink
🦄 refactor: Refactor class JavetShell
Browse files Browse the repository at this point in the history
  • Loading branch information
caoccao committed Nov 26, 2023
1 parent 7cbb82c commit 40d1ae3
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 30 deletions.
1 change: 1 addition & 0 deletions console/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ version = "0.1.0"
dependencies {
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.8.10")
implementation("com.caoccao.javet:javet:3.0.2")
implementation("com.caoccao.javet:javenode:0.3.0")

// https://github.com/Kotlin/kotlinx-cli
// https://mvnrepository.com/artifact/org.jetbrains.kotlinx/kotlinx-cli-jvm
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import com.caoccao.javet.exceptions.JavetCompilationException
import com.caoccao.javet.exceptions.JavetExecutionException
import com.caoccao.javet.interop.V8Host
import com.caoccao.javet.interop.V8Runtime
import com.caoccao.javet.interop.callback.JavetBuiltInModuleResolver
import com.caoccao.javet.shell.constants.Constants
import com.caoccao.javet.shell.entities.Options
import com.caoccao.javet.shell.enums.ExitCode
Expand All @@ -30,15 +29,17 @@ import java.io.File
import java.util.*
import java.util.concurrent.TimeUnit

class JavetShell(
private val options: Options,
abstract class BaseJavetShell(
protected val options: Options,
) : Runnable {
protected var v8Runtime: V8Runtime? = null

private var daemonThread: Thread? = null

@Volatile
private var running = false

private var v8Runtime: V8Runtime? = null
abstract val prompt: String

fun execute(): ExitCode {
println("${Constants.Application.NAME} v${Constants.Application.VERSION} (${options.jsRuntimeType.name} ${options.jsRuntimeType.version})")
Expand All @@ -47,37 +48,14 @@ class JavetShell(
V8Host.getInstance(options.jsRuntimeType).createV8Runtime<V8Runtime>().use { v8Runtime ->
running = true
this.v8Runtime = v8Runtime
if (options.jsRuntimeType.isNode) {
v8Runtime.v8ModuleResolver = JavetBuiltInModuleResolver()
v8Runtime.getExecutor(
"""const process = require('process');
process.on('unhandledRejection', (reason, promise) => {
console.error();
console.error(reason.toString());
console.error();
});"""
).executeVoid()
} else {
v8Runtime.setPromiseRejectCallback { _, _, value ->
println()
println(value.toString())
println()
}
}
registerPromiseRejectCallback()
daemonThread = Thread(this)
daemonThread?.start()
Scanner(System.`in`).use { scanner ->
val sb = StringBuilder()
var isMultiline = false
while (running) {
val prompt = if (isMultiline) {
">>> "
} else if (options.jsRuntimeType.isNode) {
"N > "
} else {
"V > "
}
print(prompt)
print(if (isMultiline) ">>> " else prompt)
try {
sb.appendLine(scanner.nextLine())
v8Runtime
Expand Down Expand Up @@ -118,6 +96,8 @@ class JavetShell(
return ExitCode.NoError
}

protected abstract fun registerPromiseRejectCallback()

override fun run() {
while (running) {
v8Runtime?.await(V8AwaitMode.RunOnce)
Expand Down
43 changes: 43 additions & 0 deletions console/src/main/kotlin/com/caoccao/javet/shell/JavetShellNode.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright (c) 2023. caoccao.com Sam Cao
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.caoccao.javet.shell

import com.caoccao.javet.interop.callback.JavetBuiltInModuleResolver
import com.caoccao.javet.shell.entities.Options

class JavetShellNode(
options: Options,
) : BaseJavetShell(options) {
init {
assert(options.jsRuntimeType.isNode) { "JS runtime type must be Node." }
}

override val prompt: String
get() = "N > "

override fun registerPromiseRejectCallback() {
v8Runtime?.v8ModuleResolver = JavetBuiltInModuleResolver()
v8Runtime?.getExecutor(
"""const process = require('process');
process.on('unhandledRejection', (reason, promise) => {
console.error();
console.error(reason.toString());
console.error();
});"""
)?.executeVoid()
}
}
38 changes: 38 additions & 0 deletions console/src/main/kotlin/com/caoccao/javet/shell/JavetShellV8.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright (c) 2023. caoccao.com Sam Cao
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.caoccao.javet.shell

import com.caoccao.javet.shell.entities.Options

class JavetShellV8(
options: Options,
) : BaseJavetShell(options) {
init {
assert(options.jsRuntimeType.isV8) { "JS runtime type must be V8." }
}

override val prompt: String
get() = "V > "

override fun registerPromiseRejectCallback() {
v8Runtime?.setPromiseRejectCallback { _, _, value ->
System.err.println()
System.err.println(value.toString())
System.err.println()
}
}
}
6 changes: 5 additions & 1 deletion console/src/main/kotlin/com/caoccao/javet/shell/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,11 @@ fun main(args: Array<String>) {
module,
scriptName,
)
val javetShell = JavetShell(options)
val javetShell = if (options.jsRuntimeType.isNode) {
JavetShellNode(options)
} else {
JavetShellV8(options)
}
val exitCode =
try {
javetShell.execute()
Expand Down

0 comments on commit 40d1ae3

Please sign in to comment.