Skip to content

Commit 8c05921

Browse files
authored
feat: Load module from direct source or file path (#21)
* handle module source being direct source or file path * bump runtime version * update changelog
1 parent 2e7bbd0 commit 8c05921

File tree

7 files changed

+27
-18
lines changed

7 files changed

+27
-18
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
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
@@ -1,6 +1,6 @@
11
[package]
22
name = "globe_runtime"
3-
version = "0.0.3"
3+
version = "0.0.4"
44
edition = "2021"
55

66
[lib]

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
# Globe Runtime
22

33
See [README.md](packages/globe_runtime/README.md)
4+
5+
![Globe Runtime](./building-the-bridge.png)

building-the-bridge.png

25.8 KB
Loading

packages/globe_runtime/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 1.0.4
2+
3+
- `GlobeRuntime.registerModule` accepts `file-path` or `JS` source string.
4+
15
## 1.0.3
26

37
- Add `JsonPayload` support to runtime.

packages/globe_runtime/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: globe_runtime
22
description: A minimalist runtime designed to leverage the JavaScript ecosystem and its tools to accelerate development in Dart.
3-
version: 1.0.3+1
3+
version: 1.0.4
44
repository: https://github.com/invertase/globe_runtime
55

66
environment:

src/lib.rs

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ pub unsafe extern "C" fn init_runtime(
5858
#[no_mangle]
5959
pub unsafe extern "C" fn register_module(
6060
module_name: *const c_char,
61-
module_full_path: *const c_char,
61+
module_source: *const c_char,
6262
error: *mut *const c_char,
6363
//
6464
args: *const *const c_void, // Arguments pointer
@@ -85,18 +85,23 @@ pub unsafe extern "C" fn register_module(
8585
}
8686
};
8787

88-
let module_path_str = match check_and_get_cstr(module_full_path) {
89-
Ok(path) => PathBuf::from(path),
90-
Err(e) => {
91-
set_error(error, e);
92-
return 1;
88+
let source_code = match check_and_get_cstr(module_source) {
89+
Ok(cstr) => {
90+
let path = PathBuf::from(cstr);
91+
if path.is_file() {
92+
match fs::read_to_string(&path) {
93+
Ok(code) => (path.to_str().unwrap().to_string(), code),
94+
Err(e) => {
95+
set_error(error, &format!("Failed to read JS module from file: {}", e));
96+
return 1;
97+
}
98+
}
99+
} else {
100+
(module_name_str.to_string() + ".js", cstr.to_string())
101+
}
93102
}
94-
};
95-
96-
let source_code = match fs::read_to_string(&module_path_str) {
97-
Ok(code) => code,
98103
Err(e) => {
99-
set_error(error, &format!("Failed to read JS module: {}", e));
104+
set_error(error, e);
100105
return 1;
101106
}
102107
};
@@ -105,10 +110,8 @@ pub unsafe extern "C" fn register_module(
105110
let mut javascript_runtime = runtime_ref.borrow_mut();
106111

107112
let module_object = {
108-
let result = javascript_runtime.lazy_load_es_module_with_code(
109-
format!("file://{}", module_path_str.to_string_lossy()),
110-
source_code,
111-
);
113+
let result = javascript_runtime
114+
.lazy_load_es_module_with_code(format!("file://{}", source_code.0), source_code.1);
112115
if let Err(e) = result {
113116
set_error(
114117
error,

0 commit comments

Comments
 (0)