From 72d53fe80760bc827d39ced3d650c144a21bb47d Mon Sep 17 00:00:00 2001 From: rmliddle Date: Sun, 28 Oct 2018 13:23:00 +1100 Subject: [PATCH] move to wasm-bindgen + supporting application --- wasm/Cargo.toml | 10 ++++++++-- wasm/app/bootstrap.js | 5 +++++ wasm/app/index.html | 10 ++++++++++ wasm/app/index.js | 3 +++ wasm/app/package.json | 27 +++++++++++++++++++++++++++ wasm/app/webpack.config.js | 14 ++++++++++++++ wasm/src/lib.rs | 28 ++++++++++++++++++++++++++++ 7 files changed, 95 insertions(+), 2 deletions(-) create mode 100644 wasm/app/bootstrap.js create mode 100644 wasm/app/index.html create mode 100644 wasm/app/index.js create mode 100644 wasm/app/package.json create mode 100644 wasm/app/webpack.config.js create mode 100644 wasm/src/lib.rs diff --git a/wasm/Cargo.toml b/wasm/Cargo.toml index 9bb6be29f..11b60624f 100644 --- a/wasm/Cargo.toml +++ b/wasm/Cargo.toml @@ -1,7 +1,10 @@ [package] name = "rustpython_wasm" version = "0.1.0" -authors = ["rmliddle "] +authors = ["Ryan Liddle "] + +[lib] +crate-type = ["cdylib", "rlib"] [workspace] members = [] @@ -9,6 +12,9 @@ members = [] [dependencies] rustpython_parser = {path = "../parser"} rustpython_vm = {path = "../vm"} +cfg-if = "0.1.2" +wasm-bindgen = "0.2" + [profile.release] -opt-level = 's' \ No newline at end of file +opt-level = "s" \ No newline at end of file diff --git a/wasm/app/bootstrap.js b/wasm/app/bootstrap.js new file mode 100644 index 000000000..726fd1b62 --- /dev/null +++ b/wasm/app/bootstrap.js @@ -0,0 +1,5 @@ +// A dependency graph that contains any wasm must all be imported +// asynchronously. This `bootstrap.js` file does the single async import, so +// that no one else needs to worry about it again. +import("./index.js") + .catch(e => console.error("Error importing `index.js`:", e)); \ No newline at end of file diff --git a/wasm/app/index.html b/wasm/app/index.html new file mode 100644 index 000000000..069a09a70 --- /dev/null +++ b/wasm/app/index.html @@ -0,0 +1,10 @@ + + + + + Hello wasm-pack! + + + + + \ No newline at end of file diff --git a/wasm/app/index.js b/wasm/app/index.js new file mode 100644 index 000000000..10797d688 --- /dev/null +++ b/wasm/app/index.js @@ -0,0 +1,3 @@ +import * as rp from "rustpython_wasm"; + +rp.run_code("print('Hello Python!')\n"); diff --git a/wasm/app/package.json b/wasm/app/package.json new file mode 100644 index 000000000..a1f9c4191 --- /dev/null +++ b/wasm/app/package.json @@ -0,0 +1,27 @@ +{ + "name": "app", + "version": "1.0.0", + "description": "", + "main": "index.js", + "dependencies": { + "hello-wasm-pack": "^0.1.0", + "webpack": "^4.16.3", + "webpack-cli": "^3.1.0", + "webpack-dev-server": "^3.1.5", + "copy-webpack-plugin": "^4.5.2" + }, + "devDependencies": {}, + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/RustPython/RustPython.git" + }, + "author": "Ryan Liddle", + "license": "MIT", + "bugs": { + "url": "https://github.com/RustPython/RustPython/issues" + }, + "homepage": "https://github.com/RustPython/RustPython#readme" +} diff --git a/wasm/app/webpack.config.js b/wasm/app/webpack.config.js new file mode 100644 index 000000000..531def733 --- /dev/null +++ b/wasm/app/webpack.config.js @@ -0,0 +1,14 @@ +const CopyWebpackPlugin = require("copy-webpack-plugin"); +const path = require('path'); + +module.exports = { + entry: "./bootstrap.js", + output: { + path: path.resolve(__dirname, "dist"), + filename: "bootstrap.js", + }, + mode: "development", + plugins: [ + new CopyWebpackPlugin(['index.html']) + ], +}; \ No newline at end of file diff --git a/wasm/src/lib.rs b/wasm/src/lib.rs new file mode 100644 index 000000000..d7916baef --- /dev/null +++ b/wasm/src/lib.rs @@ -0,0 +1,28 @@ +extern crate rustpython_vm; +extern crate wasm_bindgen; +use wasm_bindgen::prelude::*; +use rustpython_vm::VirtualMachine; +use rustpython_vm::compile; + +#[wasm_bindgen] +extern "C" { + // Use `js_namespace` here to bind `console.log(..)` instead of just + // `log(..)` + #[wasm_bindgen(js_namespace = console)] + fn log(s: &str); +} + +#[wasm_bindgen] +pub fn run_code(source: &str) -> () { + //add hash in here + log("Running RustPython"); + log(&source.to_string()); + let mut vm = VirtualMachine::new(); + let code_obj = compile::compile(&mut vm, &source.to_string(), compile::Mode::Exec, None); + let builtins = vm.get_builtin_scope(); + let vars = vm.context().new_scope(Some(builtins)); + match vm.run_code_obj(code_obj.unwrap(), vars) { + Ok(_value) => log("Execution successful"), + Err(_) => log("Execution failed") + } +} \ No newline at end of file