[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

15
src/graphics.js Normal file
View file

@ -0,0 +1,15 @@
import * as core from "graphics-core";
function cls(r, g, b) {
core.cls(r, g, b);
}
function print(...args) {
core.print(args.join(" "));
}
function spr(x, y, w, h, sx, sy, sw = undefined, sh = undefined) {
sw = sw | w;
sh = sh | h;
core.spr(xy, w, h, sx, sy, sw, sh);
}

View file

@ -1,4 +1,4 @@
import { cls, print, spr } from "graphics";
import { cls, print, spr } from "./src/graphics";
export function init() {
print("Hello world!");
@ -8,5 +8,5 @@ export function update() {}
export function draw() {
cls(0.1, 0.2, 0.3);
spr(0, 0, 0.5, 0.5, 0, 0, 0.5, 0.5);
spr(0, 0, 0.5, 0.5, 0, 0);
}

View file

@ -30,17 +30,17 @@ impl ScriptContext {
let js = include_str!("main.js");
let module = context
.load_module(js, "main.js")
.eval_module(js, "main.js")
.expect("Unable to load main");
let init = module
.get_module_export(&context, "init")
.get_export(&context, "init")
.expect("Unable to fetch init");
let update = module
.get_module_export(&context, "update")
.get_export(&context, "update")
.expect("Unable to fetch update");
let draw = module
.get_module_export(&context, "draw")
.get_export(&context, "draw")
.expect("Unable to fetch draw");
ScriptContext {

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