Skip to content

Commit

Permalink
✨ feat: Add javet.getPackage()
Browse files Browse the repository at this point in the history
  • Loading branch information
caoccao committed Dec 12, 2023
1 parent f5a5f90 commit 2b37032
Show file tree
Hide file tree
Showing 6 changed files with 165 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package com.caoccao.javet.shell
import com.caoccao.javet.enums.V8AwaitMode
import com.caoccao.javet.interfaces.IJavetClosable
import com.caoccao.javet.interop.V8Runtime
import com.caoccao.javet.javenode.JNEventLoop
import com.caoccao.javet.shell.constants.Constants
import com.caoccao.javet.shell.entities.Options
import java.util.concurrent.TimeUnit
Expand All @@ -28,6 +29,7 @@ abstract class BaseEventLoop(
protected val options: Options,
) : IJavetClosable, Runnable {
private var daemonThread: Thread? = null
protected var jnEventLoop: JNEventLoop? = null

@Volatile
var running = false
Expand Down Expand Up @@ -55,6 +57,7 @@ abstract class BaseEventLoop(
}

protected open fun start() {
jnEventLoop = JNEventLoop(v8Runtime)
running = true
daemonThread = Thread(this)
daemonThread?.start()
Expand All @@ -64,5 +67,6 @@ abstract class BaseEventLoop(
running = false
daemonThread?.join()
daemonThread = null
jnEventLoop = null
}
}
16 changes: 15 additions & 1 deletion console/src/main/kotlin/com/caoccao/javet/shell/EventLoopNode.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,24 @@ package com.caoccao.javet.shell

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

class EventLoopNode(
v8Runtime: V8Runtime,
options: Options,
) : BaseEventLoop(v8Runtime, options) {
// Node.js has its own event loop.
override fun start() {
super.start()
jnEventLoop?.loadStaticModules(
JavetShellModuleType.Javet,
)
}

override fun stop() {
jnEventLoop?.await()
jnEventLoop?.unloadStaticModules(
JavetShellModuleType.Javet,
)
super.stop()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,29 +20,28 @@ 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(
v8Runtime: V8Runtime,
options: Options,
) : BaseEventLoop(v8Runtime, options) {
private var jnEventLoop: JNEventLoop? = null

override fun start() {
jnEventLoop = JNEventLoop(v8Runtime)
super.start()
jnEventLoop?.loadStaticModules(
JNModuleType.Console,
JNModuleType.Timers,
JavetShellModuleType.Javet,
)
super.start()
}

override fun stop() {
jnEventLoop?.await()
jnEventLoop?.unloadStaticModules(
JNModuleType.Console,
JNModuleType.Timers,
JavetShellModuleType.Javet,
)
jnEventLoop = null
super.stop()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* 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.enums

import com.caoccao.javet.javenode.enums.JNModuleType
import com.caoccao.javet.shell.modules.javet.JavetModule

object JavetShellModuleType {
val Javet = JNModuleType(JavetModule.NAME, ::JavetModule)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* 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.modules.javet

import com.caoccao.javet.interop.callback.IJavetDirectCallable
import com.caoccao.javet.interop.callback.JavetCallbackContext
import com.caoccao.javet.interop.callback.JavetCallbackType
import com.caoccao.javet.javenode.JNEventLoop
import com.caoccao.javet.javenode.modules.BaseJNModule
import com.caoccao.javet.shell.enums.JavetShellModuleType
import com.caoccao.javet.values.V8Value
import com.caoccao.javet.values.primitive.V8ValueString

class JavetModule(eventLoop: JNEventLoop) : BaseJNModule(eventLoop), IJavetDirectCallable {
companion object {
const val NAME = "javet"
}

override fun getType() = JavetShellModuleType.Javet

override fun getCallbackContexts(): Array<JavetCallbackContext> {
if (javetCallbackContexts == null) {
javetCallbackContexts = arrayOf(
JavetCallbackContext(
"getPackage",
this, JavetCallbackType.DirectCallNoThisAndResult,
IJavetDirectCallable.NoThisAndResult<Exception>(this::getPackage),
)
)
}
return javetCallbackContexts
}

override fun bind() {
v8Runtime.createV8ValueObject().use { v8ValueObject ->
bind(v8ValueObject)
v8Runtime.globalObject.set(NAME, v8ValueObject)
}
}

fun getPackage(v8Values: Array<V8Value>): V8Value {
if (v8Values.isNotEmpty()) {
val v8Value = v8Values[0]
if (v8Value is V8ValueString) {
val packageName = v8Value.value
if (packageName.isNotBlank()) {
return JavetPackage(v8Runtime, packageName).toV8Value()
}
}
}
return v8Runtime.createV8ValueUndefined()
}

override fun unbind() {
v8Runtime.globalObject.delete(NAME)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* 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.modules.javet

import com.caoccao.javet.interop.V8Runtime
import com.caoccao.javet.interop.callback.IJavetDirectCallable
import com.caoccao.javet.interop.callback.JavetCallbackContext
import com.caoccao.javet.interop.callback.JavetCallbackType
import com.caoccao.javet.javenode.modules.BaseJNCallable
import com.caoccao.javet.values.V8Value

class JavetPackage(private val v8Runtime: V8Runtime, val name: String) : BaseJNCallable(), IJavetDirectCallable {
override fun getCallbackContexts(): Array<JavetCallbackContext> {
if (javetCallbackContexts == null) {
javetCallbackContexts = arrayOf(
JavetCallbackContext(
"name",
this, JavetCallbackType.DirectCallGetterAndNoThis,
IJavetDirectCallable.GetterAndNoThis<Exception>(this::getName),
)
)
}
return javetCallbackContexts
}

fun getName() = v8Runtime.createV8ValueString(name)

fun toV8Value(): V8Value {
val v8ValueObject = v8Runtime.createV8ValueObject()
v8ValueObject.bind(this)
return v8ValueObject
}
}

0 comments on commit 2b37032

Please sign in to comment.