-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(graalvm): add
structuredClone
support
feat(graalvm): add `structuredClone` support test(graalvm): add `structuredClone` tests Closes #1267 Relates-to: #1267 Signed-off-by: Sam Gammon <sam@elide.dev>
- Loading branch information
Showing
5 changed files
with
162 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
packages/graalvm/src/main/kotlin/elide/runtime/javascript/StructuredCloneBuiltin.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
* Copyright (c) 2024-2025 Elide Technologies, Inc. | ||
* | ||
* Licensed under the MIT license (the "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* https://opensource.org/license/mit/ | ||
* | ||
* 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. | ||
*/ | ||
@file:OptIn(DelicateElideApi::class) | ||
|
||
package elide.runtime.javascript | ||
|
||
import org.graalvm.polyglot.Context | ||
import org.graalvm.polyglot.Source | ||
import org.graalvm.polyglot.Value | ||
import org.graalvm.polyglot.proxy.ProxyExecutable | ||
import jakarta.inject.Singleton | ||
import elide.runtime.core.DelicateElideApi | ||
import elide.runtime.gvm.api.Intrinsic | ||
import elide.runtime.gvm.internals.intrinsics.js.AbstractJsIntrinsic | ||
import elide.runtime.gvm.js.JsError | ||
import elide.runtime.gvm.js.JsSymbol.JsSymbols.asPublicJsSymbol | ||
import elide.runtime.intrinsics.GuestIntrinsic | ||
|
||
// Name of the `structuredClone` function in the global scope. | ||
private const val CLONE_FN_NAME = "structuredClone" | ||
|
||
// Public JavaScript symbol for the `structuredClone` function. | ||
private val CLONE_FN_SYMBOL = CLONE_FN_NAME.asPublicJsSymbol() | ||
|
||
/** | ||
* ## Structured Clone Built-in | ||
* | ||
* Implements the `structuredClone` global function for JavaScript, which is used to create a deep copy of an object, | ||
* usually for the purpose of transferring it between different execution contexts. | ||
* | ||
* ### Standards Compliance | ||
* | ||
* The Navigator `structuredClone` function is defined as part of the WinterTC Minimum Common API. | ||
* | ||
* [MDN](https://developer.mozilla.org/en-US/docs/Web/API/Window/structuredClone) | ||
*/ | ||
@Singleton | ||
@Intrinsic(CLONE_FN_NAME) public class StructuredCloneBuiltin : ProxyExecutable, AbstractJsIntrinsic() { | ||
// language=JavaScript | ||
private val structuredCloner = """ | ||
function doStructuredClone(value) { | ||
return JSON.parse(JSON.stringify(value)); | ||
} | ||
doStructuredClone; | ||
""".trimIndent() | ||
|
||
private val cloner = Source.newBuilder("js", structuredCloner, "structuredCloner.js") | ||
.internal(true) | ||
.cached(true) | ||
.build() | ||
|
||
override fun install(bindings: GuestIntrinsic.MutableIntrinsicBindings) { | ||
bindings[CLONE_FN_SYMBOL] = this | ||
} | ||
|
||
public fun clone(value: Value, forContext: Context): Value { | ||
val cloner = forContext.eval(cloner) | ||
assert(cloner.canExecute()) { "Structured cloner function is not executable" } | ||
return requireNotNull(cloner.execute(value)) { "Failed to clone value" } | ||
} | ||
|
||
override fun execute(vararg arguments: Value?): Any? { | ||
val value = arguments.firstOrNull() ?: throw JsError.typeError("First argument to `structuredClone` is required") | ||
if (value.canExecute()) throw JsError.typeError("Cannot clone functions") | ||
return clone(value, value.context) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
packages/graalvm/src/test/kotlin/elide/runtime/javascript/StructuredCloneTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
* Copyright (c) 2024-2025 Elide Technologies, Inc. | ||
* | ||
* Licensed under the MIT license (the "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* https://opensource.org/license/mit/ | ||
* | ||
* 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. | ||
*/ | ||
@file:OptIn(DelicateElideApi::class) | ||
|
||
package elide.runtime.javascript | ||
|
||
import org.graalvm.polyglot.Value | ||
import org.junit.jupiter.api.assertNotNull | ||
import kotlin.test.assertEquals | ||
import kotlin.test.assertFalse | ||
import kotlin.test.assertNotSame | ||
import kotlin.test.assertTrue | ||
import elide.annotations.Inject | ||
import elide.runtime.core.DelicateElideApi | ||
import elide.runtime.gvm.internals.js.AbstractJsIntrinsicTest | ||
import elide.runtime.plugins.js.javascript | ||
import elide.testing.annotations.Test | ||
import elide.testing.annotations.TestCase | ||
|
||
@TestCase internal class StructuredCloneTest : AbstractJsIntrinsicTest<StructuredCloneBuiltin>() { | ||
@Inject lateinit var structuredClone: StructuredCloneBuiltin | ||
override fun provide(): StructuredCloneBuiltin = structuredClone | ||
|
||
override fun testInjectable() { | ||
assertNotNull(structuredClone) | ||
} | ||
|
||
@Test fun testCloneSimple() { | ||
val (value, cloned) = withContext { | ||
val value = javascript( | ||
// language=JavaScript | ||
""" | ||
const x = { a: 1, b: 2 }; | ||
x; | ||
""".trimIndent() | ||
) | ||
value to structuredClone.clone(Value.asValue(value), unwrap()) | ||
} | ||
assertNotNull(cloned) | ||
assertTrue(cloned.hasMembers()) | ||
assertTrue(cloned.hasMember("a")) | ||
assertTrue(cloned.hasMember("b")) | ||
assertFalse(cloned.hasMember("c")) | ||
assertEquals(1, cloned.getMember("a").asInt()) | ||
assertEquals(2, cloned.getMember("b").asInt()) | ||
assertEquals(2, cloned.memberKeys.size) | ||
assertNotSame(value, cloned) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters