Skip to content

Commit ca30ebc

Browse files
committed
Genericize the wasm lib to not be specifically for the demo
This included changing RustObjectKind::RustFunction.function to be a `Box<Fn()>` instead of a `fn()` to support closures.
1 parent 4c32693 commit ca30ebc

22 files changed

Lines changed: 10523 additions & 91 deletions

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ version = "0.0.1"
44
authors = ["Windel Bouwman", "Shing Lyu <shing.lyu@gmail.com>"]
55

66
[workspace]
7-
members = [".", "vm", "wasm", "parser"]
7+
members = [".", "vm", "wasm/lib", "parser"]
88

99
[dependencies]
1010
log="0.4.1"

vm/src/frame.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -559,7 +559,7 @@ impl Frame {
559559
bytecode::Instruction::LoadBuildClass => {
560560
let rustfunc = PyObject::new(
561561
PyObjectKind::RustFunction {
562-
function: builtins::builtin_build_class_,
562+
function: Box::new(builtins::builtin_build_class_),
563563
},
564564
vm.ctx.type_type(),
565565
);

vm/src/pyobject.rs

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -452,9 +452,24 @@ impl PyContext {
452452
)
453453
}
454454

455-
pub fn new_rustfunc(&self, function: RustPyFunc) -> PyObjectRef {
455+
pub fn new_rustfunc<F: 'static + Fn(&mut VirtualMachine, PyFuncArgs) -> PyResult>(
456+
&self,
457+
function: F,
458+
) -> PyObjectRef {
456459
PyObject::new(
457-
PyObjectKind::RustFunction { function: function },
460+
PyObjectKind::RustFunction {
461+
function: Box::new(function),
462+
},
463+
self.function_type(),
464+
)
465+
}
466+
467+
pub fn new_rustfunc_from_box(
468+
&self,
469+
function: Box<(Fn(&mut VirtualMachine, PyFuncArgs) -> PyResult)>,
470+
) -> PyObjectRef {
471+
PyObject::new(
472+
PyObjectKind::RustFunction { function },
458473
self.function_type(),
459474
)
460475
}
@@ -463,7 +478,10 @@ impl PyContext {
463478
PyObject::new(PyObjectKind::Frame { frame: frame }, self.frame_type())
464479
}
465480

466-
pub fn new_property(&self, function: RustPyFunc) -> PyObjectRef {
481+
pub fn new_property<F: 'static + Fn(&mut VirtualMachine, PyFuncArgs) -> PyResult>(
482+
&self,
483+
function: F,
484+
) -> PyObjectRef {
467485
let fget = self.new_rustfunc(function);
468486
let py_obj = PyObject::new(
469487
PyObjectKind::Instance {
@@ -505,7 +523,10 @@ impl PyContext {
505523
)
506524
}
507525

508-
pub fn new_member_descriptor(&self, function: RustPyFunc) -> PyObjectRef {
526+
pub fn new_member_descriptor<F: 'static + Fn(&mut VirtualMachine, PyFuncArgs) -> PyResult>(
527+
&self,
528+
function: F,
529+
) -> PyObjectRef {
509530
let dict = self.new_dict();
510531
self.set_item(&dict, "function", self.new_rustfunc(function));
511532
self.new_instance(dict, self.member_descriptor_type())
@@ -762,8 +783,6 @@ impl PyFuncArgs {
762783
}
763784
}
764785

765-
type RustPyFunc = fn(vm: &mut VirtualMachine, PyFuncArgs) -> PyResult;
766-
767786
/// Rather than determining the type of a python object, this enum is more
768787
/// a holder for the rust payload of a python object. It is more a carrier
769788
/// of rust data for a particular python object. Determine the python type
@@ -840,7 +859,7 @@ pub enum PyObjectKind {
840859
dict: PyObjectRef,
841860
},
842861
RustFunction {
843-
function: RustPyFunc,
862+
function: Box<(Fn(&mut VirtualMachine, PyFuncArgs) -> PyResult)>,
844863
},
845864
}
846865

vm/src/vm.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ impl VirtualMachine {
229229
pub fn invoke(&mut self, func_ref: PyObjectRef, args: PyFuncArgs) -> PyResult {
230230
trace!("Invoke: {:?} {:?}", func_ref, args);
231231
match func_ref.borrow().kind {
232-
PyObjectKind::RustFunction { function } => function(self, args),
232+
PyObjectKind::RustFunction { ref function } => function(self, args),
233233
PyObjectKind::Function {
234234
ref code,
235235
ref scope,

wasm/build.sh

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
wasm-pack build --debug && \
2-
cd pkg && \
3-
npm link && \
4-
cd ../app && \
5-
npm install && \
6-
npm link rustpython_wasm && \
7-
node_modules/.bin/webpack-dev-server
1+
set -e
2+
(cd lib; wasm-pack build --debug)
3+
(cd demo; npm install && node_modules/.bin/webpack-dev-server)

0 commit comments

Comments
 (0)