Skip to content

Commit 84a20cf

Browse files
committed
feat: adds inline script support to Graal engine.
1 parent d659511 commit 84a20cf

File tree

1 file changed

+44
-12
lines changed

1 file changed

+44
-12
lines changed

scripting/graalvm/src/main/java/io/gatehill/imposter/scripting/graalvm/service/GraalvmScriptServiceImpl.kt

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,10 @@ class GraalvmScriptServiceImpl : ScriptService, Plugin {
9696
}
9797
}
9898

99+
override fun initInlineScript(scriptId: String, scriptCode: String) {
100+
LOGGER.trace("No op inline script init for script with ID: $scriptId")
101+
}
102+
99103
override fun executeScript(
100104
script: ScriptSource,
101105
runtimeContext: RuntimeContext,
@@ -104,29 +108,57 @@ class GraalvmScriptServiceImpl : ScriptService, Plugin {
104108

105109
val wrapped = JavaScriptUtil.wrapScript(script)
106110

107-
Context.newBuilder(JS_LANG_ID)
108-
.engine(engine)
109-
.allowHostAccess(HostAccess.ALL)
110-
.allowHostClassLookup { _ -> true }
111-
.build()
112-
.use { context ->
111+
buildContext().use { context ->
112+
val bindings = context.getBindings(JS_LANG_ID)
113+
JavaScriptUtil.transformRuntimeMap(
114+
runtimeContext,
115+
addDslPrefix = true,
116+
addConsoleShim = false
117+
).map { (key, value) ->
118+
bindings.putMember(key, value)
119+
}
120+
121+
val result = context.eval(JS_LANG_ID, wrapped.code)
122+
val dsl = result.`as`(Dsl::class.java)
123+
return dsl.responseBehaviour
124+
}
125+
} catch (e: Exception) {
126+
throw RuntimeException("Script execution terminated abnormally", e)
127+
}
128+
129+
override fun evalInlineScript(
130+
scriptId: String,
131+
scriptCode: String,
132+
runtimeContext: RuntimeContext,
133+
): Boolean {
134+
LOGGER.trace("Executing inline script: {}", scriptId)
135+
136+
try {
137+
buildContext().use { context ->
113138
val bindings = context.getBindings(JS_LANG_ID)
114139
JavaScriptUtil.transformRuntimeMap(
115140
runtimeContext,
116-
addDslPrefix = true,
141+
addDslPrefix = false,
117142
addConsoleShim = false
118143
).map { (key, value) ->
119144
bindings.putMember(key, value)
120145
}
121146

122-
val result = context.eval(JS_LANG_ID, wrapped.code)
123-
val dsl = result.`as`(Dsl::class.java)
124-
return dsl.responseBehaviour
147+
val result = context.eval(JS_LANG_ID, scriptCode)
148+
val resultValue = result.`as`(Any::class.java)
149+
return resultValue is Boolean && resultValue
125150
}
126-
} catch (e: Exception) {
127-
throw RuntimeException("Script execution terminated abnormally", e)
151+
} catch (e: Exception) {
152+
throw RuntimeException("Inline script evaluation terminated abnormally", e)
153+
}
128154
}
129155

156+
private fun buildContext(): Context = Context.newBuilder(JS_LANG_ID)
157+
.engine(engine)
158+
.allowHostAccess(HostAccess.ALL)
159+
.allowHostClassLookup { _ -> true }
160+
.build()
161+
130162
companion object {
131163
private val LOGGER = LogManager.getLogger(GraalvmScriptServiceImpl::class.java)
132164
private const val JS_LANG_ID = "js"

0 commit comments

Comments
 (0)