Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit72d53fe

Browse files
committed
move to wasm-bindgen + supporting application
1 parent1c37625 commit72d53fe

File tree

7 files changed

+95
-2
lines changed

7 files changed

+95
-2
lines changed

‎wasm/Cargo.toml‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
[package]
22
name ="rustpython_wasm"
33
version ="0.1.0"
4-
authors = ["rmliddle <ryan@rmliddle.com>"]
4+
authors = ["Ryan Liddle <ryan@rmliddle.com>"]
5+
6+
[lib]
7+
crate-type = ["cdylib","rlib"]
58

69
[workspace]
710
members = []
811

912
[dependencies]
1013
rustpython_parser = {path ="../parser"}
1114
rustpython_vm = {path ="../vm"}
15+
cfg-if ="0.1.2"
16+
wasm-bindgen ="0.2"
17+
1218

1319
[profile.release]
14-
opt-level ='s'
20+
opt-level ="s"

‎wasm/app/bootstrap.js‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// A dependency graph that contains any wasm must all be imported
2+
// asynchronously. This `bootstrap.js` file does the single async import, so
3+
// that no one else needs to worry about it again.
4+
import("./index.js")
5+
.catch(e=>console.error("Error importing `index.js`:",e));

‎wasm/app/index.html‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<metacharset="utf-8">
5+
<title>Hello wasm-pack!</title>
6+
</head>
7+
<body>
8+
<scriptsrc="./bootstrap.js"></script>
9+
</body>
10+
</html>

‎wasm/app/index.js‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import*asrpfrom"rustpython_wasm";
2+
3+
rp.run_code("print('Hello Python!')\n");

‎wasm/app/package.json‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"name":"app",
3+
"version":"1.0.0",
4+
"description":"",
5+
"main":"index.js",
6+
"dependencies": {
7+
"hello-wasm-pack":"^0.1.0",
8+
"webpack":"^4.16.3",
9+
"webpack-cli":"^3.1.0",
10+
"webpack-dev-server":"^3.1.5",
11+
"copy-webpack-plugin":"^4.5.2"
12+
},
13+
"devDependencies": {},
14+
"scripts": {
15+
"test":"echo\"Error: no test specified\" && exit 1"
16+
},
17+
"repository": {
18+
"type":"git",
19+
"url":"git+https://github.com/RustPython/RustPython.git"
20+
},
21+
"author":"Ryan Liddle",
22+
"license":"MIT",
23+
"bugs": {
24+
"url":"https://github.com/RustPython/RustPython/issues"
25+
},
26+
"homepage":"https://github.com/RustPython/RustPython#readme"
27+
}

‎wasm/app/webpack.config.js‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
constCopyWebpackPlugin=require("copy-webpack-plugin");
2+
constpath=require('path');
3+
4+
module.exports={
5+
entry:"./bootstrap.js",
6+
output:{
7+
path:path.resolve(__dirname,"dist"),
8+
filename:"bootstrap.js",
9+
},
10+
mode:"development",
11+
plugins:[
12+
newCopyWebpackPlugin(['index.html'])
13+
],
14+
};

‎wasm/src/lib.rs‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
externcrate rustpython_vm;
2+
externcrate wasm_bindgen;
3+
use wasm_bindgen::prelude::*;
4+
use rustpython_vm::VirtualMachine;
5+
use rustpython_vm::compile;
6+
7+
#[wasm_bindgen]
8+
extern"C"{
9+
// Use `js_namespace` here to bind `console.log(..)` instead of just
10+
// `log(..)`
11+
#[wasm_bindgen(js_namespace = console)]
12+
fnlog(s:&str);
13+
}
14+
15+
#[wasm_bindgen]
16+
pubfnrun_code(source:&str) ->(){
17+
//add hash in here
18+
log("Running RustPython");
19+
log(&source.to_string());
20+
letmut vm =VirtualMachine::new();
21+
let code_obj = compile::compile(&mut vm,&source.to_string(), compile::Mode::Exec,None);
22+
let builtins = vm.get_builtin_scope();
23+
let vars = vm.context().new_scope(Some(builtins));
24+
match vm.run_code_obj(code_obj.unwrap(), vars){
25+
Ok(_value) =>log("Execution successful"),
26+
Err(_) =>log("Execution failed")
27+
}
28+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp