mirror of
https://github.com/RustPython/RustPython.git
synced 2026-06-09 22:49:57 +09:00
move to wasm-bindgen + supporting application
This commit is contained in:
@@ -1,7 +1,10 @@
|
||||
[package]
|
||||
name = "rustpython_wasm"
|
||||
version = "0.1.0"
|
||||
authors = ["rmliddle <ryan@rmliddle.com>"]
|
||||
authors = ["Ryan Liddle <ryan@rmliddle.com>"]
|
||||
|
||||
[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'
|
||||
opt-level = "s"
|
||||
5
wasm/app/bootstrap.js
vendored
Normal file
5
wasm/app/bootstrap.js
vendored
Normal file
@@ -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));
|
||||
10
wasm/app/index.html
Normal file
10
wasm/app/index.html
Normal file
@@ -0,0 +1,10 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>Hello wasm-pack!</title>
|
||||
</head>
|
||||
<body>
|
||||
<script src="./bootstrap.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
3
wasm/app/index.js
Normal file
3
wasm/app/index.js
Normal file
@@ -0,0 +1,3 @@
|
||||
import * as rp from "rustpython_wasm";
|
||||
|
||||
rp.run_code("print('Hello Python!')\n");
|
||||
27
wasm/app/package.json
Normal file
27
wasm/app/package.json
Normal file
@@ -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"
|
||||
}
|
||||
14
wasm/app/webpack.config.js
Normal file
14
wasm/app/webpack.config.js
Normal file
@@ -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'])
|
||||
],
|
||||
};
|
||||
28
wasm/src/lib.rs
Normal file
28
wasm/src/lib.rs
Normal file
@@ -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")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user