Skip to content

Commit 0a70981

Browse files
committed
feat(graalvm): add support for MessageChannel
feat: add `MessageChannel` interface and basic impl feat: add `MessagePort` interface and basic impl test: add initial tests for channel messaging test: add `MessageChannel` and `MessagePort` to common minimum chore: re-pin api for `graalvm` module Fixes and closes #1317 Relates-to: #1317 Signed-off-by: Sam Gammon <[email protected]>
1 parent 9e6a50c commit 0a70981

File tree

4 files changed

+69
-10
lines changed

4 files changed

+69
-10
lines changed

crates/js/src/lib.rs

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ use diag::{DiagnosticBuilder, create_diagnostic, report_diagnostic};
1818
use java_native::jni;
1919
use jni::JNIEnv;
2020
use jni::objects::{JClass, JString};
21+
use jni::sys::jboolean;
22+
use oxc::codegen::CodegenOptions;
23+
use oxc::parser::ParseOptions;
24+
use oxc::span::SourceType;
25+
use oxc::transformer::TransformOptions;
2126

2227
/// Code generation tools for JavaScript; interoperates with `parser` and other exposed modules.
2328
mod codegen;
@@ -31,12 +36,43 @@ pub fn precompile<'a>(
3136
_class: JClass<'a>,
3237
name: JString<'a>,
3338
code: JString<'a>,
39+
typescript: jboolean,
40+
jsx: jboolean,
41+
esm: jboolean,
3442
) -> JString<'a> {
3543
let str = env.get_string(&code).unwrap();
3644
let code = str.to_str().to_string();
3745
let filename = env.get_string(&name).unwrap().to_str().to_string();
38-
let result = codegen::lower_into(GeneratorOptions::default(), filename, code.as_str());
46+
let default_options = GeneratorOptions::default();
47+
let source_type = if typescript == jni::sys::JNI_TRUE {
48+
if jsx == jni::sys::JNI_TRUE {
49+
Some(SourceType::tsx())
50+
} else {
51+
Some(SourceType::ts())
52+
}
53+
} else {
54+
if jsx == jni::sys::JNI_TRUE {
55+
Some(SourceType::jsx())
56+
} else {
57+
if esm == jni::sys::JNI_TRUE {
58+
Some(SourceType::mjs())
59+
} else {
60+
Some(SourceType::cjs())
61+
}
62+
}
63+
};
64+
let generatorOptions = GeneratorOptions {
65+
source_type,
66+
parser: Some(ParseOptions::default()),
67+
codegen: Some(CodegenOptions::default()),
68+
transform: Some(TransformOptions::default()),
69+
..default_options
70+
};
71+
72+
// fire the precompiler
73+
let result = codegen::lower_into(generatorOptions, filename, code.as_str());
3974

75+
// process errors, which may be non-fatal; such errors are raised as static diagnostics.
4076
match result {
4177
Ok(output) => env.new_string(output.code).unwrap(),
4278

packages/graalvm-js/api/graalvm-js.api

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,18 @@ public class elide/runtime/lang/javascript/JSRealmPatcher {
5454
public final class elide/runtime/lang/javascript/JavaScriptCompilerConfig : elide/runtime/precompiler/Precompiler$Configuration {
5555
public static final field Companion Lelide/runtime/lang/javascript/JavaScriptCompilerConfig$Companion;
5656
public fun <init> ()V
57-
public fun <init> (Z)V
58-
public synthetic fun <init> (ZILkotlin/jvm/internal/DefaultConstructorMarker;)V
57+
public fun <init> (ZZZ)V
58+
public synthetic fun <init> (ZZZILkotlin/jvm/internal/DefaultConstructorMarker;)V
5959
public final fun component1 ()Z
60-
public final fun copy (Z)Lelide/runtime/lang/javascript/JavaScriptCompilerConfig;
61-
public static synthetic fun copy$default (Lelide/runtime/lang/javascript/JavaScriptCompilerConfig;ZILjava/lang/Object;)Lelide/runtime/lang/javascript/JavaScriptCompilerConfig;
60+
public final fun component2 ()Z
61+
public final fun component3 ()Z
62+
public final fun copy (ZZZ)Lelide/runtime/lang/javascript/JavaScriptCompilerConfig;
63+
public static synthetic fun copy$default (Lelide/runtime/lang/javascript/JavaScriptCompilerConfig;ZZZILjava/lang/Object;)Lelide/runtime/lang/javascript/JavaScriptCompilerConfig;
6264
public fun equals (Ljava/lang/Object;)Z
6365
public static final fun getDEFAULT ()Lelide/runtime/lang/javascript/JavaScriptCompilerConfig;
66+
public final fun getJsx ()Z
6467
public final fun getSourceMaps ()Z
68+
public final fun getTypescript ()Z
6569
public fun hashCode ()I
6670
public fun toString ()Ljava/lang/String;
6771
}

packages/graalvm-js/src/main/kotlin/elide/runtime/lang/javascript/JavaScriptCompilerConfig.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,17 @@ import elide.runtime.precompiler.Precompiler
1616

1717
/**
1818
* ## Java Script Compiler Config
19+
*
20+
* @property sourceMaps Whether to generate source maps.
21+
* @property jsx Whether to support JSX.
22+
* @property esm Whether to support ECMAScript modules.
23+
* @property typescript Whether to support TypeScript.
1924
*/
2025
public data class JavaScriptCompilerConfig(
2126
val sourceMaps: Boolean = true,
27+
val jsx: Boolean = true,
28+
val esm: Boolean = true,
29+
val typescript: Boolean = true,
2230
) : Precompiler.Configuration {
2331
public companion object {
2432
/** Default JavaScript compiler configuration. */

packages/graalvm-js/src/main/kotlin/elide/runtime/lang/javascript/JavaScriptPrecompiler.kt

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
*/
1313
package elide.runtime.lang.javascript
1414

15-
import org.graalvm.nativeimage.ImageInfo
16-
import kotlinx.atomicfu.atomic
1715
import elide.annotations.API
1816
import elide.runtime.core.lib.NativeLibraries
1917
import elide.runtime.diag.Diagnostics
@@ -52,7 +50,7 @@ public object JavaScriptPrecompiler : Precompiler.SourcePrecompiler<JavaScriptCo
5250
@Suppress("TooGenericExceptionCaught")
5351
override fun invoke(req: PrecompileSourceRequest<JavaScriptCompilerConfig>, input: String): String? = try {
5452
initialize()
55-
precompile(req.source.name, input).also {
53+
precompile(req.config, req.source.name, input).also {
5654
// after running the precompiler, consume all matching diagnostics (if present), and throw them back to the caller
5755
if (Diagnostics.dirty(LANG_TAG_JS)) Diagnostics.query(LANG_TAG_JS, TOOL_PRECOMPILER, true).let { diag ->
5856
PrecompilerNotice.Companion.from(DiagnosticsContainer.Companion.from(diag))
@@ -62,17 +60,30 @@ public object JavaScriptPrecompiler : Precompiler.SourcePrecompiler<JavaScriptCo
6260
throw PrecompilerError("Precompile failed: ${err.javaClass.simpleName} ${err.message}", err)
6361
}
6462

63+
// Precompile with assigned configuration.
64+
private fun precompile(config: JavaScriptCompilerConfig, name: String, input: String): String? {
65+
val ts = config.typescript
66+
val jsx = config.jsx
67+
val esm = config.esm || name.endsWith(".mjs")
68+
return precompile(name, input, ts, jsx, esm)
69+
}
70+
6571
/**
66-
* ## Precompile JavaScript
72+
* ## Precompile JavaScript/TypeScript
6773
*
6874
* This method accepts code in the form of JSX, TSX, TypeScript, or JavaScript, and will act to parse it using the
6975
* native parser, and then lower it into compliant ECMA code which is runnable by Elide.
7076
*
7177
* @param name Filename for the code provided herein
7278
* @param code Code contents from the file in question
79+
* @param ts Whether the code is expected to support TypeScript types or not
80+
* @param jsx Whether the code is expected to support JSX/TSX or not
81+
* @param esm Whether the code is expected to be an ES Module or not
7382
* @return Lowered source-code which should be used instead
7483
*/
75-
@JvmStatic @JvmName("precompile") private external fun precompile(name: String, code: String): String?
84+
@JvmStatic
85+
@JvmName("precompile")
86+
private external fun precompile(name: String, code: String, ts: Boolean, jsx: Boolean, esm: Boolean): String?
7687

7788
/** Provider for the [JavaScriptPrecompiler]. */
7889
public class Provider : Precompiler.Provider<JavaScriptPrecompiler> {

0 commit comments

Comments
 (0)