[oden][oden-js] Rework modules

Damn this is a lot
This commit is contained in:
John Doty 2023-06-24 08:45:39 -07:00
parent aa90cea4a3
commit db8a5f8eed
12 changed files with 280 additions and 105 deletions

View file

@ -1,4 +1,4 @@
use oden_js::{module, ContextRef, Value, ValueRef, ValueResult};
use oden_js::{module, ContextRef, Value, ValueResult};
use std::sync::mpsc::Sender;
use std::sync::Arc;
@ -37,13 +37,7 @@ impl GraphicsImpl {
GraphicsImpl { sender }
}
fn print_fn(&self, ctx: &ContextRef, args: &[&ValueRef]) -> ValueResult {
let mut text = String::with_capacity(128);
for arg in args {
let v = arg.to_string(ctx)?;
text.push_str(&v);
}
fn print_fn(&self, ctx: &ContextRef, text: String) -> ValueResult {
let _ = self
.sender
.send(GraphicsCommand::Print(PrintCommand { text }));
@ -67,8 +61,8 @@ impl GraphicsImpl {
h: f32,
u: f32,
v: f32,
sw: Option<f32>,
sh: Option<f32>,
sw: f32,
sh: f32,
) -> ValueResult {
let _ = self.sender.send(GraphicsCommand::Sprite(SpriteCommand {
x,
@ -77,8 +71,8 @@ impl GraphicsImpl {
h,
u,
v,
sw: sw.unwrap_or(w),
sh: sh.unwrap_or(h),
sw,
sh,
}));
Ok(Value::undefined(ctx))
}
@ -91,12 +85,12 @@ pub struct GraphicsAPI {
impl GraphicsAPI {
pub fn define(ctx: &ContextRef, sender: Sender<GraphicsCommand>) -> oden_js::Result<Self> {
let gfx = Arc::new(GraphicsImpl::new(sender));
let mut builder = module::NativeModuleBuilder::new(ctx);
let mut builder = module::native::NativeModuleBuilder::new(ctx);
{
let gfx = gfx.clone();
builder.export(
"print",
ctx.new_dynamic_fn(move |ctx, _, args| gfx.print_fn(ctx, args))?,
ctx.new_fn(move |ctx: &ContextRef, t: String| gfx.print_fn(ctx, t))?,
)?;
}
{
@ -120,14 +114,14 @@ impl GraphicsAPI {
h: f32,
u: f32,
v: f32,
sw: Option<f32>,
sh: Option<f32>| {
sw: f32,
sh: f32| {
gfx.spr_fn(ctx, x, y, w, h, u, v, sw, sh)
},
)?,
)?;
}
builder.build("graphics")?;
builder.build("graphics-core")?;
Ok(GraphicsAPI { gfx })
}