diff --git a/oden-js-sys/src/static-functions.rs b/oden-js-sys/src/static-functions.rs index da619269..6419d46e 100644 --- a/oden-js-sys/src/static-functions.rs +++ b/oden-js-sys/src/static-functions.rs @@ -50,6 +50,8 @@ extern "C" { magic: ::std::os::raw::c_int, ) -> JSValue; fn JS_MakeException_real() -> JSValue; + fn JS_MakeNull_real() -> JSValue; + fn JS_MakeUndefined_real() -> JSValue; fn JS_ValueGetPtr_real(v: JSValue) -> *mut ::std::os::raw::c_void; } @@ -218,6 +220,14 @@ pub unsafe fn JS_MakeException() -> JSValue { JS_MakeException_real() } +pub unsafe fn JS_MakeNull() -> JSValue { + JS_MakeNull_real() +} + +pub unsafe fn JS_MakeUndefined() -> JSValue { + JS_MakeUndefined_real() +} + pub unsafe fn JS_ValueGetPtr(v: JSValue) -> *mut ::std::os::raw::c_void { JS_ValueGetPtr_real(v) } diff --git a/oden-js-sys/static-functions.c b/oden-js-sys/static-functions.c index 4aec6a10..26922beb 100644 --- a/oden-js-sys/static-functions.c +++ b/oden-js-sys/static-functions.c @@ -129,6 +129,14 @@ JSValue JS_MakeException_real() { return JS_EXCEPTION; } +JSValue JS_MakeNull_real() { + return JS_NULL; +} + +JSValue JS_MakeUndefined_real() { + return JS_UNDEFINED; +} + void *JS_ValueGetPtr_real(JSValue val) { return JS_VALUE_GET_PTR(val); } diff --git a/oden-js/src/value.rs b/oden-js/src/value.rs index dbebce06..b6d88405 100644 --- a/oden-js/src/value.rs +++ b/oden-js/src/value.rs @@ -328,6 +328,18 @@ impl ValueRef { ctx.check_exception(sys::JS_GetModuleExport(ctx.ctx, module, c_value.into_raw())) } } + + pub fn call(&self, ctx: &ContextRef) -> Result { + unsafe { + ctx.check_exception(sys::JS_Call( + ctx.ctx, + self.val, + sys::JS_MakeUndefined(), + 0, + std::ptr::null_mut(), + )) + } + } } impl<'ctx> fmt::Debug for ValueRef { @@ -357,6 +369,20 @@ impl Value { rt: Runtime::from_raw(unsafe { sys::JS_GetRuntime(ctx.ctx) }), } } + + pub fn null(ctx: &ContextRef) -> Self { + Value { + value: ValueRef::from_raw(unsafe { sys::JS_MakeNull() }), + rt: Runtime::from_raw(unsafe { sys::JS_GetRuntime(ctx.ctx) }), + } + } + + pub fn undefined(ctx: &ContextRef) -> Self { + Value { + value: ValueRef::from_raw(unsafe { sys::JS_MakeUndefined() }), + rt: Runtime::from_raw(unsafe { sys::JS_GetRuntime(ctx.ctx) }), + } + } } impl Deref for Value { diff --git a/src/lib.rs b/src/lib.rs index 5314c44b..562f9e35 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -356,6 +356,8 @@ pub async fn run() { let mut state = State::new(window).await; let context = script::ScriptContext::new(); + context.init(); + event_loop.run(move |event, _, control_flow| { control_flow.set_poll(); diff --git a/src/main.js b/src/main.js new file mode 100644 index 00000000..7b985b1f --- /dev/null +++ b/src/main.js @@ -0,0 +1,9 @@ +function init() { + // console.log("Hello world!"); +} + +function update() {} + +function draw() {} + +export { init, update, draw } diff --git a/src/script.rs b/src/script.rs index 2ed49e87..4391e0d2 100644 --- a/src/script.rs +++ b/src/script.rs @@ -1,7 +1,10 @@ use oden_js as js; pub struct ScriptContext { - _context: js::Context, + context: js::Context, + init: js::Value, + update: js::Value, + draw: js::Value, } impl ScriptContext { @@ -13,10 +16,40 @@ impl ScriptContext { context.add_intrinsic_bigdecimal(); context.add_intrinsic_operators(); - ScriptContext { _context: context } + let js = include_str!("main.js"); + let module = context + .load_module(js, "main.js") + .expect("Unable to load main"); + + let init = module + .get_module_export(&context, "init") + .expect("Unable to fetch init"); + let update = module + .get_module_export(&context, "update") + .expect("Unable to fetch update"); + let draw = module + .get_module_export(&context, "draw") + .expect("Unable to fetch draw"); + + ScriptContext { + context, + init, + update, + draw, + } } - pub fn update(&self) {} + pub fn init(&self) { + self.init.call(&self.context).expect("Exception in init"); + } - pub fn render(&self) {} + pub fn update(&self) { + self.update + .call(&self.context) + .expect("Exception in update"); + } + + pub fn render(&self) { + self.draw.call(&self.context).expect("Exception in draw"); + } }