[oden] Graphics is a module now

This commit is contained in:
John Doty 2023-06-21 06:33:39 -07:00
parent c574fd8cb8
commit 611322d9a3
2 changed files with 10 additions and 29 deletions

View file

@ -1,8 +1,14 @@
use oden_js::{Class, ClassID, Context, ContextRef, Runtime, Value, ValueRef, ValueResult};
use oden_js::{module, Context, ContextRef, Runtime, Value, ValueRef, ValueResult};
pub struct GraphicsAPI {}
impl GraphicsAPI {
fn define(ctx: &ContextRef) -> oden_js::Result<()> {
module::NativeModuleBuilder::new(ctx)
.export("print", ctx.new_dynamic_fn(Self::print_fn)?)?
.build("graphics")
}
fn print_fn(ctx: &ContextRef, _this: &ValueRef, args: &[&ValueRef]) -> ValueResult {
for arg in args {
print!("{}", arg.to_string(ctx)?);
@ -12,22 +18,6 @@ impl GraphicsAPI {
}
}
impl Class for GraphicsAPI {
fn class_id() -> &'static ClassID {
static ID: ClassID = ClassID::new("GraphicsAPI");
&ID
}
fn prototype(context: &ContextRef) -> ValueResult {
let mut prototype = context.new_object()?;
let print = context.new_dynamic_fn(GraphicsAPI::print_fn)?;
prototype.set_property(context, "print", &print)?;
Ok(prototype)
}
}
pub struct ScriptContext {
context: Context,
init: Value,
@ -44,17 +34,7 @@ impl ScriptContext {
context.add_intrinsic_bigdecimal();
context.add_intrinsic_operators();
// TODO: This should be a module instead. Yuck.
let mut global = context
.global_object()
.expect("Unable to get global object");
let graphics = GraphicsAPI {};
let graphics = graphics
.into_value(&context)
.expect("Unable to wrap graphics API");
global
.set_property(&context, "graphics", &graphics)
.expect("Unable to establish graphics");
GraphicsAPI::define(&context).expect("Graphics module should load without error");
let js = include_str!("main.js");
let module = context