From 0c71f7d4c4363cb221f84c974173f1aea463cbf4 Mon Sep 17 00:00:00 2001 From: Ning Sun Date: Sat, 24 Jun 2017 17:22:13 +0800 Subject: [PATCH] (feat) initial work on making a wasm base handlebars library for javascript Signed-off-by: Ning Sun --- .gitignore | 4 ++++ Cargo.toml | 5 ++--- index.html | 10 ++++++++++ js/wasm.js | 12 ++++++++++++ package.json | 33 +++++++++++++++++++++++++++++++++ src/wasm.rs | 31 +++++++++++++++++++++++++++++++ webpack.config.js | 28 ++++++++++++++++++++++++++++ 7 files changed, 120 insertions(+), 3 deletions(-) create mode 100644 index.html create mode 100644 js/wasm.js create mode 100644 package.json create mode 100644 src/wasm.rs create mode 100644 webpack.config.js diff --git a/.gitignore b/.gitignore index bcfafe938..31f87e29a 100644 --- a/.gitignore +++ b/.gitignore @@ -14,3 +14,7 @@ Cargo.lock # Generated by oprofile oprofile_data + +# NPM files +node_modules +build diff --git a/Cargo.toml b/Cargo.toml index b4615d604..a0dfd8423 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,10 +12,9 @@ repository = "https://github.com/sunng87/handlebars-rust" documentation = "https://docs.rs/handlebars/" readme = "README.md" -[lib] - +[[bin]] name = "handlebars" -path = "src/lib.rs" +path = "src/wasm.rs" [dependencies] diff --git a/index.html b/index.html new file mode 100644 index 000000000..5a9887017 --- /dev/null +++ b/index.html @@ -0,0 +1,10 @@ + + + + Hello World + + +
+ + + diff --git a/js/wasm.js b/js/wasm.js new file mode 100644 index 000000000..174b39a5f --- /dev/null +++ b/js/wasm.js @@ -0,0 +1,12 @@ +const wasm = require('../src/wasm.rs'); + +wasm.initialize().then(module => { + console.log('Calling ccall'); + module.ccall('compile', null, ['string', 'string'], ['test', 'hello {{world}}']); + const add = module.cwrap('add', 'number', ['number', 'number']) + const compile = module.cwrap('compile', null, ['string', 'string']) + + console.log('Calling rust functions from javascript!') + console.log(add(1, 5)) + compile('test', 'hello {{world}}'); +}) diff --git a/package.json b/package.json new file mode 100644 index 000000000..653ed94ae --- /dev/null +++ b/package.json @@ -0,0 +1,33 @@ +{ + "name": "handlebars-wasm", + "version": "1.0.0", + "description": "handlebars templating via wasm", + "main": "index.js", + "directories": { + "example": "examples" + }, + "scripts": { + "compile": "webpack --progress", + "serve": "http-server" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/sunng87/handlebars-rust.git" + }, + "keywords": [ + "wasm", + "handlebars", + "templating" + ], + "author": "Ning Sun", + "license": "MIT", + "bugs": { + "url": "https://github.com/sunng87/handlebars-rust/issues" + }, + "homepage": "https://github.com/sunng87/handlebars-rust#readme", + "devDependencies": { + "http-server": "^0.10.0", + "rust-wasm-loader": "^0.1.2", + "webpack": "^3.0.0" + } +} diff --git a/src/wasm.rs b/src/wasm.rs new file mode 100644 index 000000000..6f39ba621 --- /dev/null +++ b/src/wasm.rs @@ -0,0 +1,31 @@ +extern crate handlebars; +#[macro_use] +extern crate lazy_static; + +use std::sync::Mutex; +use handlebars::Handlebars; + +lazy_static! { + static ref DEFAULT_REGISTRY: Mutex = Mutex::new(Handlebars::new()); +} + +#[no_mangle] +pub fn compile(name: String, template: String) { + println!("compiling template: {:?} {:?}", name, template); + DEFAULT_REGISTRY + .lock() + .unwrap() + .register_template_string(&name, &template) + .unwrap(); + println!("compiled {:?}", name); +} + +#[no_mangle] +pub fn add(a: i32, b: i32) -> i32 { + a + b +} + + +fn main() { + println!("loaded."); +} diff --git a/webpack.config.js b/webpack.config.js new file mode 100644 index 000000000..609fed0c8 --- /dev/null +++ b/webpack.config.js @@ -0,0 +1,28 @@ +module.exports = { + entry: './js/wasm.js', + output: { + filename: 'bundle.js', + path: __dirname + '/build', + }, + module: { + rules: [ + { + test: /\.rs$/, + use: { + loader: 'rust-wasm-loader', + options: { + // The path to the webpack output relative to the project root + path: 'build' + } + } + } + ] + }, + // The .wasm 'glue' code generated by Emscripten requires these node builtins, + // but won't actually use them in a web environment. We tell Webpack to not resolve those + // require statements since we know we won't need them. + externals: { + 'fs': true, + 'path': true, + } +}