Skip to content

Commit ce17eb9

Browse files
authored
feat: change extensions from scripts to modules (#139)
1 parent 8a616eb commit ce17eb9

File tree

22 files changed

+156
-87
lines changed

22 files changed

+156
-87
lines changed

Cargo.lock

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ authors = ["the Andromeda team"]
77
edition = "2024"
88
license = "Mozilla Public License 2.0"
99
repository = "https://github.com/tryandromeda/andromeda"
10-
version = "0.1.0-draft35"
10+
version = "0.1.0-draft36"
1111

1212
[workspace.dependencies]
1313
andromeda-core = { path = "core" }

core/src/extension.rs

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ use nova_vm::{
66
ecmascript::{
77
builtins::{Behaviour, BuiltinFunctionArgs, RegularFn, create_builtin_function},
88
execution::Agent,
9-
scripts_and_modules::script::{parse_script, script_evaluation},
9+
scripts_and_modules::{
10+
module::module_semantics::source_text_module_records::parse_module, script::HostDefined,
11+
},
1012
types::{InternalMethods, IntoValue, Object, PropertyDescriptor, PropertyKey},
1113
},
1214
engine::context::{Bindable, GcScope},
@@ -57,32 +59,34 @@ impl Extension {
5759
andromeda_object: Object,
5860
gc: &mut GcScope<'_, '_>,
5961
) {
60-
for file in &self.files {
61-
let source_text = nova_vm::ecmascript::types::String::from_str(agent, file, gc.nogc());
62-
let script = match parse_script(
62+
for (idx, file_source) in self.files.iter().enumerate() {
63+
let specifier = format!("<ext:{}:{}>", self.name, idx);
64+
let source_text =
65+
nova_vm::ecmascript::types::String::from_str(agent, file_source, gc.nogc());
66+
67+
let module = match parse_module(
6368
agent,
6469
source_text,
6570
agent.current_realm(gc.nogc()),
66-
true,
67-
None,
71+
Some(std::rc::Rc::new(specifier.clone()) as HostDefined),
6872
gc.nogc(),
6973
) {
70-
Ok(script) => script,
71-
Err(diagnostics) => exit_with_parse_errors(diagnostics, "<runtime>", file),
74+
Ok(module) => module,
75+
Err(diagnostics) => exit_with_parse_errors(diagnostics, &specifier, file_source),
7276
};
73-
let eval_result = script_evaluation(agent, script.unbind(), gc.reborrow()).unbind();
74-
match eval_result {
75-
Ok(_) => (),
76-
Err(e) => {
77-
let error_value = e.value();
78-
let message = error_value
79-
.string_repr(agent, gc.reborrow())
80-
.as_str(agent)
81-
.unwrap_or("<non-string error>")
82-
.to_string();
83-
let err = AndromedaError::runtime_error(message);
84-
print_enhanced_error(&err);
85-
}
77+
78+
let eval_result = agent
79+
.run_parsed_module(module.unbind(), None, gc.reborrow())
80+
.unbind();
81+
if let Err(e) = eval_result {
82+
let error_value = e.value();
83+
let message = error_value
84+
.string_repr(agent, gc.reborrow())
85+
.as_str(agent)
86+
.unwrap_or("<non-string error>")
87+
.to_string();
88+
let err = AndromedaError::runtime_error(message);
89+
print_enhanced_error(&err);
8690
}
8791
}
8892
for op in &self.ops {

runtime/src/ext/broadcast_channel/broadcast_channel.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,3 +281,5 @@ class BroadcastChannel {
281281
}, inspectOptions);
282282
}
283283
}
284+
285+
globalThis.BroadcastChannel = BroadcastChannel;

runtime/src/ext/canvas/image.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,6 @@ function createImageBitmap(path: string): ImageBitmap {
5252
const height = __andromeda__.internal_image_bitmap_get_height(rid);
5353
return new ImageBitmap(rid, width, height);
5454
}
55+
56+
globalThis.createImageBitmap = createImageBitmap;
57+
globalThis.ImageBitmap = ImageBitmap;

runtime/src/ext/canvas/mod.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
// License, v. 2.0. If a copy of the MPL was not distributed with this
33
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
44

5-
// deno-lint-ignore-file no-unused-vars
6-
75
/**
86
* A OffscreenCanvas implementation
97
*/
@@ -419,3 +417,5 @@ class CanvasGradient {
419417
);
420418
}
421419
}
420+
421+
globalThis.OffscreenCanvas = OffscreenCanvas;

runtime/src/ext/console/mod.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -674,5 +674,5 @@ const andromedaConsole = {
674674
},
675675
};
676676

677-
// Export the console object
677+
// // Export the console object
678678
globalThis.console = andromedaConsole;

runtime/src/ext/crypto/mod.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
// License, v. 2.0. If a copy of the MPL was not distributed with this
33
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
44

5-
// deno-lint-ignore-file no-unused-vars
6-
75
// Web Crypto API implementation following W3C specification
86

97
/**
@@ -416,3 +414,6 @@ const crypto = {
416414
return __andromeda__.internal_crypto_randomUUID();
417415
},
418416
};
417+
418+
// @ts-ignore globalThis is not readonly
419+
globalThis.crypto = crypto;

runtime/src/ext/ffi/mod.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// This Source Code Form is subject to the terms of the Mozilla Public
22
// License, v. 2.0. If a copy of the MPL was not distributed with this
33
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
4-
// deno-lint-ignore-file no-unused-vars
54

65
/**
76
* All plain number types for interfacing with foreign functions.
@@ -626,3 +625,14 @@ function dlopen<T extends ForeignLibraryInterface>(
626625

627626
return new DynamicLibrary(libId, callableSymbols);
628627
}
628+
629+
// @ts-ignore globalThis is not readonly
630+
globalThis.dlopen = dlopen;
631+
// @ts-ignore globalThis is not readonly
632+
globalThis.UnsafeCallback = UnsafeCallback;
633+
// @ts-ignore globalThis is not readonly
634+
globalThis.UnsafeFnPointer = UnsafeFnPointer;
635+
// @ts-ignore globalThis is not readonly
636+
globalThis.UnsafePointer = UnsafePointer;
637+
// @ts-ignore globalThis is not readonly
638+
globalThis.UnsafePointerView = UnsafePointerView;

runtime/src/ext/file/blob.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,3 +195,6 @@ class Blob {
195195
return this.#blobId;
196196
}
197197
}
198+
199+
// @ts-ignore globalThis is not readonly
200+
globalThis.Blob = Blob;

0 commit comments

Comments
 (0)