Skip to content

Commit

Permalink
Add stdlib
Browse files Browse the repository at this point in the history
  • Loading branch information
WilliamRagstad committed Aug 16, 2024
1 parent 4de8af3 commit 13cdc49
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
11 changes: 11 additions & 0 deletions src/engine/stdlib.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
((globalThis) => {
globalThis.webx = {
log: (...args) => {
Deno.core.print(`[out]: ${argsToMessage(...args)}\n`, false);
},
error: (...args) => {
Deno.core.print(`[err]: ${argsToMessage(...args)}\n`, true);
},
static: (path) => Deno.readTextFileSync(path)
};
})(globalThis);
25 changes: 24 additions & 1 deletion src/engine/stdlib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use deno_core::{error::AnyError, extension, include_js_files, op2, Extension};

use crate::reporting::error::ERROR_HANDLER_CALL;

use super::runtime::{WXRTValue, WXRuntimeError, WXRuntimeInfo};
Expand All @@ -23,11 +25,13 @@ fn webx_static(
}
}
Err(WXRuntimeError {
message: format!("static: failed to read file '{}'", relative_path.to_js()),
message: format!("static: failed to read file '{}'", relative_path.to_raw()),
code: ERROR_HANDLER_CALL,
})
}

/// Try to call a native function by name. \
/// TODO: Figure out if this should be replaced with a JS extension.
pub fn try_call(
name: &str,
args: &[WXRTValue],
Expand All @@ -51,3 +55,22 @@ pub fn try_call(
_ => None,
}
}

// #[op]
// async fn op_webx_static(relative_path: String) -> Result<String, AnyError> {
// let file = std::fs::read_to_string(relative_path).await?;
// Ok(file)
// }

pub fn init() -> Extension {
Extension {
name: "webx stdlib",
ops: vec![].into(), // vec![op_webx_static::decl()],
esm_files: include_js_files!(stdlib "src/engine/stdlib.js",)
.to_vec()
.into(),
..Default::default()
}
}

pub const JAVASCRIPT: &str = include_str!("./stdlib.js");

0 comments on commit 13cdc49

Please sign in to comment.