Compare commits
2 commits
db8a5f8eed
...
b2b730f79c
| Author | SHA1 | Date | |
|---|---|---|---|
| b2b730f79c | |||
| 9e685bc569 |
4 changed files with 20 additions and 12 deletions
|
|
@ -9,7 +9,7 @@ pub enum ModuleSource<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait ModuleLoader {
|
pub trait ModuleLoader {
|
||||||
fn load(&mut self, context: &ContextRef, name: &str) -> Result<ModuleSource>;
|
fn load(&self, context: &ContextRef, name: &str) -> Result<ModuleSource>;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct DefaultModuleLoader {}
|
pub struct DefaultModuleLoader {}
|
||||||
|
|
@ -21,7 +21,7 @@ impl DefaultModuleLoader {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ModuleLoader for DefaultModuleLoader {
|
impl ModuleLoader for DefaultModuleLoader {
|
||||||
fn load(&mut self, _context: &ContextRef, name: &str) -> Result<ModuleSource> {
|
fn load(&self, _context: &ContextRef, name: &str) -> Result<ModuleSource> {
|
||||||
// Attempt to open the file.
|
// Attempt to open the file.
|
||||||
let path = Path::new(name);
|
let path = Path::new(name);
|
||||||
match std::fs::read_to_string(path) {
|
match std::fs::read_to_string(path) {
|
||||||
|
|
@ -34,7 +34,7 @@ impl ModuleLoader for DefaultModuleLoader {
|
||||||
pub(crate) fn load_module(
|
pub(crate) fn load_module(
|
||||||
context: &ContextRef,
|
context: &ContextRef,
|
||||||
name: &str,
|
name: &str,
|
||||||
loader: &mut Box<dyn ModuleLoader>,
|
loader: &Box<dyn ModuleLoader>,
|
||||||
) -> *mut sys::JSModuleDef {
|
) -> *mut sys::JSModuleDef {
|
||||||
match loader.load(context, name) {
|
match loader.load(context, name) {
|
||||||
Ok(ModuleSource::Native(native)) => native.module,
|
Ok(ModuleSource::Native(native)) => native.module,
|
||||||
|
|
|
||||||
|
|
@ -3,10 +3,11 @@ use crate::ContextRef;
|
||||||
use oden_js_sys as sys;
|
use oden_js_sys as sys;
|
||||||
use std::cell::RefCell;
|
use std::cell::RefCell;
|
||||||
use std::ffi::CStr;
|
use std::ffi::CStr;
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
struct PrivateState {
|
struct PrivateState {
|
||||||
refs: u64,
|
refs: u64,
|
||||||
loader: Box<dyn ModuleLoader>,
|
loader: Arc<Box<dyn ModuleLoader>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl PrivateState {
|
impl PrivateState {
|
||||||
|
|
@ -20,9 +21,16 @@ impl PrivateState {
|
||||||
Err(_) => return std::ptr::null_mut(),
|
Err(_) => return std::ptr::null_mut(),
|
||||||
};
|
};
|
||||||
|
|
||||||
let state = opaque as *mut PrivateState;
|
let loader = unsafe {
|
||||||
|
let ptr = opaque as *const RefCell<PrivateState>;
|
||||||
|
ptr.as_ref()
|
||||||
|
.expect("We already know this runtime is one of ours!")
|
||||||
|
.borrow()
|
||||||
|
.loader
|
||||||
|
.clone()
|
||||||
|
};
|
||||||
let context = ContextRef::from_raw(ctx);
|
let context = ContextRef::from_raw(ctx);
|
||||||
load_module(&context, path, &mut (*state).loader)
|
load_module(&context, path, &loader)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -39,7 +47,7 @@ impl Runtime {
|
||||||
pub fn with_loader<TLoader: ModuleLoader + 'static>(loader: TLoader) -> Runtime {
|
pub fn with_loader<TLoader: ModuleLoader + 'static>(loader: TLoader) -> Runtime {
|
||||||
let state = Box::new(RefCell::new(PrivateState {
|
let state = Box::new(RefCell::new(PrivateState {
|
||||||
refs: 1,
|
refs: 1,
|
||||||
loader: Box::new(loader),
|
loader: Arc::new(Box::new(loader)),
|
||||||
}));
|
}));
|
||||||
let rt = unsafe {
|
let rt = unsafe {
|
||||||
let rt = sys::JS_NewRuntime();
|
let rt = sys::JS_NewRuntime();
|
||||||
|
|
|
||||||
|
|
@ -1,15 +1,15 @@
|
||||||
import * as core from "graphics-core";
|
import * as core from "graphics-core";
|
||||||
|
|
||||||
function cls(r, g, b) {
|
export function cls(r, g, b) {
|
||||||
core.cls(r, g, b);
|
core.cls(r, g, b);
|
||||||
}
|
}
|
||||||
|
|
||||||
function print(...args) {
|
export function print(...args) {
|
||||||
core.print(args.join(" "));
|
core.print(args.join(" "));
|
||||||
}
|
}
|
||||||
|
|
||||||
function spr(x, y, w, h, sx, sy, sw = undefined, sh = undefined) {
|
export function spr(x, y, w, h, sx, sy, sw = undefined, sh = undefined) {
|
||||||
sw = sw | w;
|
sw = sw | w;
|
||||||
sh = sh | h;
|
sh = sh | h;
|
||||||
core.spr(xy, w, h, sx, sy, sw, sh);
|
core.spr(x, y, w, h, sx, sy, sw, sh);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
import { cls, print, spr } from "./src/graphics";
|
import { cls, print, spr } from "./src/graphics.js";
|
||||||
|
|
||||||
export function init() {
|
export function init() {
|
||||||
print("Hello world!");
|
print("Hello world!");
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue