[oden] JavaScript, god help me

This commit is contained in:
John Doty 2023-06-19 13:46:09 -07:00
parent 16e6f1304c
commit 8d7dd789ed
6 changed files with 92 additions and 4 deletions

View file

@ -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)
}

View file

@ -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);
}

View file

@ -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<Value> {
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 {

View file

@ -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();

9
src/main.js Normal file
View file

@ -0,0 +1,9 @@
function init() {
// console.log("Hello world!");
}
function update() {}
function draw() {}
export { init, update, draw }

View file

@ -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 render(&self) {}
pub fn init(&self) {
self.init.call(&self.context).expect("Exception in init");
}
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");
}
}