JS package (WIP)

This commit is contained in:
Pavel Murygin 2022-01-21 19:06:06 +03:00
parent 50c809e60c
commit 79a2a89607
11 changed files with 7612 additions and 9 deletions

18
Cargo.lock generated
View File

@ -869,9 +869,9 @@ dependencies = [
[[package]] [[package]]
name = "fluence-it-types" name = "fluence-it-types"
version = "0.3.1" version = "0.3.1"
source = "git+https://github.com/fluencelabs/interface-types?branch=marine_web_experiments#2e742a9fe1c857f622c4f0d3e105dec548bc53cd" source = "git+https://github.com/fluencelabs/interface-types/?branch=marine_web_experiments#2e742a9fe1c857f622c4f0d3e105dec548bc53cd"
dependencies = [ dependencies = [
"it-to-bytes 0.1.0 (git+https://github.com/fluencelabs/interface-types?branch=marine_web_experiments)", "it-to-bytes 0.1.0 (git+https://github.com/fluencelabs/interface-types/?branch=marine_web_experiments)",
"nom", "nom",
"serde", "serde",
"variant_count", "variant_count",
@ -1361,9 +1361,9 @@ dependencies = [
[[package]] [[package]]
name = "it-lilo" name = "it-lilo"
version = "0.2.0" version = "0.2.0"
source = "git+https://github.com/fluencelabs/interface-types?branch=marine_web_experiments#2e742a9fe1c857f622c4f0d3e105dec548bc53cd" source = "git+https://github.com/fluencelabs/interface-types/?branch=marine_web_experiments#2e742a9fe1c857f622c4f0d3e105dec548bc53cd"
dependencies = [ dependencies = [
"fluence-it-types 0.3.1 (git+https://github.com/fluencelabs/interface-types?branch=marine_web_experiments)", "fluence-it-types 0.3.1 (git+https://github.com/fluencelabs/interface-types/?branch=marine_web_experiments)",
"it-traits", "it-traits",
"log", "log",
"paste", "paste",
@ -1379,12 +1379,12 @@ checksum = "729c74bb4236418898a219c6d96f14cba77456dd7c04a2e99e65e9c643709b56"
[[package]] [[package]]
name = "it-to-bytes" name = "it-to-bytes"
version = "0.1.0" version = "0.1.0"
source = "git+https://github.com/fluencelabs/interface-types?branch=marine_web_experiments#2e742a9fe1c857f622c4f0d3e105dec548bc53cd" source = "git+https://github.com/fluencelabs/interface-types/?branch=marine_web_experiments#2e742a9fe1c857f622c4f0d3e105dec548bc53cd"
[[package]] [[package]]
name = "it-traits" name = "it-traits"
version = "0.1.0" version = "0.1.0"
source = "git+https://github.com/fluencelabs/interface-types?branch=marine_web_experiments#2e742a9fe1c857f622c4f0d3e105dec548bc53cd" source = "git+https://github.com/fluencelabs/interface-types/?branch=marine_web_experiments#2e742a9fe1c857f622c4f0d3e105dec548bc53cd"
dependencies = [ dependencies = [
"thiserror", "thiserror",
] ]
@ -3669,11 +3669,11 @@ dependencies = [
[[package]] [[package]]
name = "wasmer-interface-types-fl" name = "wasmer-interface-types-fl"
version = "0.21.0" version = "0.21.0"
source = "git+https://github.com/fluencelabs/interface-types?branch=marine_web_experiments#2e742a9fe1c857f622c4f0d3e105dec548bc53cd" source = "git+https://github.com/fluencelabs/interface-types/?branch=marine_web_experiments#2e742a9fe1c857f622c4f0d3e105dec548bc53cd"
dependencies = [ dependencies = [
"fluence-it-types 0.3.1 (git+https://github.com/fluencelabs/interface-types?branch=marine_web_experiments)", "fluence-it-types 0.3.1 (git+https://github.com/fluencelabs/interface-types/?branch=marine_web_experiments)",
"it-lilo 0.2.0", "it-lilo 0.2.0",
"it-to-bytes 0.1.0 (git+https://github.com/fluencelabs/interface-types?branch=marine_web_experiments)", "it-to-bytes 0.1.0 (git+https://github.com/fluencelabs/interface-types/?branch=marine_web_experiments)",
"it-traits", "it-traits",
"itertools 0.10.3", "itertools 0.10.3",
"log", "log",

View File

@ -1,3 +1,5 @@
#! /bin/bash
wasm-pack build -d marine-web-pkg --target web wasm-pack build -d marine-web-pkg --target web
wasm-pack build -d marine-node-pkg --target nodejs wasm-pack build -d marine-node-pkg --target nodejs
wasm-pack build --no-typescript --release -d marine-any-pkg wasm-pack build --no-typescript --release -d marine-any-pkg

9
web-runtime/js-package/.gitignore vendored Normal file
View File

@ -0,0 +1,9 @@
dist
node_modules
wasm
*.tgz
# this file is auto-generated
src/wasm.js
src/importObject.ts
src/avm.wasm

View File

@ -0,0 +1,8 @@
module.exports = {
semi: true,
trailingComma: 'all',
singleQuote: true,
printWidth: 120,
tabWidth: 4,
useTabs: false,
};

7161
web-runtime/js-package/package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,29 @@
{
"name": "@fluencelabs/marine-js",
"description": "Aquamarine VM",
"version": "0.0.1-snapshot",
"main": "./dist/index.js",
"typings": "./dist/index.d.ts",
"repository": "https://github.com/fluencelabs/air",
"author": "Fluence Labs",
"license": "Apache 2.0",
"files": [
"dist/*"
],
"bin": {
"copy-avm": "./dist/copyAvm.js"
},
"scripts": {
"build": "tsc",
"postbuild": "cp ../marine-web-pkg/marine_web_runtime_bg.wasm ./dist/marine-js.wasm",
"test": "jest"
},
"private": false,
"devDependencies": {
"@types/jest": "^27.4.0",
"@types/node": "^14.0.0",
"jest": "^27.2.4",
"ts-jest": "^27.0.5",
"typescript": "^4.0.0"
}
}

View File

@ -0,0 +1,5 @@
describe("Tests", () => {
it("should work", async () => {
expect(1 + 2).toBe(3);
});
});

View File

@ -0,0 +1 @@
export * from './marine_web_runtime';

View File

@ -0,0 +1,90 @@
/*
module.exports.call_export = function (instance, export_name, args) {
//console.log("JS: call_export called with: ", instance, export_name, args)
let parsed_args = JSON.parse(args);
//console.log("parsed args: ", args);
let prepared_args = [];
for (let i = 0; i < parsed_args.length; i++) {
let arg = parsed_args[i];
// console.log(arg)
prepared_args.push(arg["I32"])
}
//console.log("prepared args: ", prepared_args);
let result = instance.exports[export_name](...prepared_args);
//console.log("got result: ", result)
let json_string = "[]";
if (result !== undefined) {
json_string = "[" + JSON.stringify(result) + "]"
}
//console.log("got result_string: ", json_string)
return json_string
}
module.exports.get_memory_size = function (instance) {
//console.log("called get_memory_size with name=", module_name);
let buf = new Uint8Array(instance.exports.memory.buffer);
//console.log("result=", buf.byteLength);
return buf.byteLength
}
module.exports.write_byte = function (instance, offset, value) {
//console.log("write_byte called with args: module_name={}, offset={}, value={}", module_name, offset, value)
let buf = new Uint8Array(instance.exports.memory.buffer);
//console.log(buf)
buf[offset] = value
}
module.exports.read_byte = function (instance, offset) {
//console.log("read_byte called with args: module_name={}, offset={}", module_name, offset)
let buf = new Uint8Array(instance.exports.memory.buffer);
//console.log(buf)
//console.log("read_byte returns {}", buf[offset])
return buf[offset];
}
*/
export function call_export(instance, export_name, args) {
//console.log("JS: call_export called with: ", instance, export_name, args)
let parsed_args = JSON.parse(args);
//console.log("parsed args: ", args);
let prepared_args = [];
for (let i = 0; i < parsed_args.length; i++) {
let arg = parsed_args[i];
// console.log(arg)
prepared_args.push(arg["I32"])
}
//console.log("prepared args: ", prepared_args);
let result = instance.exports[export_name](...prepared_args);
//console.log("got result: ", result)
let json_string = "[]";
if (result !== undefined) {
json_string = "[" + JSON.stringify(result) + "]"
}
//console.log("got result_string: ", json_string)
return json_string
}
export function get_memory_size(instance) {
//console.log("called get_memory_size with name=", module_name);
let buf = new Uint8Array(instance.exports.memory.buffer);
//console.log("result=", buf.byteLength);
return buf.byteLength
}
export function write_byte(instance, offset, value) {
//console.log("write_byte called with args: module_name={}, offset={}, value={}", module_name, offset, value)
let buf = new Uint8Array(instance.exports.memory.buffer);
//console.log(buf)
buf[offset] = value
}
export function read_byte(instance, offset) {
//console.log("read_byte called with args: module_name={}, offset={}", module_name, offset)
let buf = new Uint8Array(instance.exports.memory.buffer);
//console.log(buf)
//console.log("read_byte returns {}", buf[offset])
return buf[offset];
}

View File

@ -0,0 +1,272 @@
import {
call_export,
read_byte,
write_byte,
get_memory_size,
} from './snippets/marine-web-runtime-6faa67b8af9cc173/marine-js.js';
async function init(module) {
let wasm;
const heap = new Array(32).fill(undefined);
heap.push(undefined, null, true, false);
function getObject(idx) {
return heap[idx];
}
let heap_next = heap.length;
function dropObject(idx) {
if (idx < 36) return;
heap[idx] = heap_next;
heap_next = idx;
}
function takeObject(idx) {
const ret = getObject(idx);
dropObject(idx);
return ret;
}
let cachedTextDecoder = new TextDecoder('utf-8', {
ignoreBOM: true,
fatal: true,
});
cachedTextDecoder.decode();
let cachegetUint8Memory0 = null;
function getUint8Memory0() {
if (cachegetUint8Memory0 === null || cachegetUint8Memory0.buffer !== wasm.memory.buffer) {
cachegetUint8Memory0 = new Uint8Array(wasm.memory.buffer);
}
return cachegetUint8Memory0;
}
function getStringFromWasm0(ptr, len) {
return cachedTextDecoder.decode(getUint8Memory0().subarray(ptr, ptr + len));
}
function addHeapObject(obj) {
if (heap_next === heap.length) heap.push(heap.length + 1);
const idx = heap_next;
heap_next = heap[idx];
heap[idx] = obj;
return idx;
}
let WASM_VECTOR_LEN = 0;
let cachedTextEncoder = new TextEncoder('utf-8');
const encodeString =
typeof cachedTextEncoder.encodeInto === 'function'
? function (arg, view) {
return cachedTextEncoder.encodeInto(arg, view);
}
: function (arg, view) {
const buf = cachedTextEncoder.encode(arg);
view.set(buf);
return {
read: arg.length,
written: buf.length,
};
};
function passStringToWasm0(arg, malloc, realloc) {
if (realloc === undefined) {
const buf = cachedTextEncoder.encode(arg);
const ptr = malloc(buf.length);
getUint8Memory0()
.subarray(ptr, ptr + buf.length)
.set(buf);
WASM_VECTOR_LEN = buf.length;
return ptr;
}
let len = arg.length;
let ptr = malloc(len);
const mem = getUint8Memory0();
let offset = 0;
for (; offset < len; offset++) {
const code = arg.charCodeAt(offset);
if (code > 0x7f) break;
mem[ptr + offset] = code;
}
if (offset !== len) {
if (offset !== 0) {
arg = arg.slice(offset);
}
ptr = realloc(ptr, len, (len = offset + arg.length * 3));
const view = getUint8Memory0().subarray(ptr + offset, ptr + len);
const ret = encodeString(arg, view);
offset += ret.written;
}
WASM_VECTOR_LEN = offset;
return ptr;
}
let cachegetInt32Memory0 = null;
function getInt32Memory0() {
if (cachegetInt32Memory0 === null || cachegetInt32Memory0.buffer !== wasm.memory.buffer) {
cachegetInt32Memory0 = new Int32Array(wasm.memory.buffer);
}
return cachegetInt32Memory0;
}
/**
* @param {string} name
*/
function greet(name) {
var ptr0 = passStringToWasm0(name, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
var len0 = WASM_VECTOR_LEN;
wasm.greet(ptr0, len0);
}
function passArray8ToWasm0(arg, malloc) {
const ptr = malloc(arg.length * 1);
getUint8Memory0().set(arg, ptr / 1);
WASM_VECTOR_LEN = arg.length;
return ptr;
}
/**
* @param {string} name
* @param {Uint8Array} wit_section_bytes
* @param {any} wasm_instance
*/
function register_module(name, wit_section_bytes, wasm_instance) {
var ptr0 = passStringToWasm0(name, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
var len0 = WASM_VECTOR_LEN;
var ptr1 = passArray8ToWasm0(wit_section_bytes, wasm.__wbindgen_malloc);
var len1 = WASM_VECTOR_LEN;
wasm.register_module(ptr0, len0, ptr1, len1, addHeapObject(wasm_instance));
}
/**
* @param {string} module_name
* @param {string} function_name
* @param {string} args
* @returns {string}
*/
function call_module(module_name, function_name, args) {
try {
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
var ptr0 = passStringToWasm0(module_name, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
var len0 = WASM_VECTOR_LEN;
var ptr1 = passStringToWasm0(function_name, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
var len1 = WASM_VECTOR_LEN;
var ptr2 = passStringToWasm0(args, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
var len2 = WASM_VECTOR_LEN;
wasm.call_module(retptr, ptr0, len0, ptr1, len1, ptr2, len2);
var r0 = getInt32Memory0()[retptr / 4 + 0];
var r1 = getInt32Memory0()[retptr / 4 + 1];
return getStringFromWasm0(r0, r1);
} finally {
wasm.__wbindgen_add_to_stack_pointer(16);
wasm.__wbindgen_free(r0, r1);
}
}
async function load(module, imports) {
if (typeof Response === 'function' && module instanceof Response) {
if (typeof WebAssembly.instantiateStreaming === 'function') {
try {
return await WebAssembly.instantiateStreaming(module, imports);
} catch (e) {
if (module.headers.get('Content-Type') != 'application/wasm') {
console.warn(
'`WebAssembly.instantiateStreaming` failed because your server does not serve wasm with `application/wasm` MIME type. Falling back to `WebAssembly.instantiate` which is slower. Original error:\n',
e,
);
} else {
throw e;
}
}
}
const bytes = await module.arrayBuffer();
return await WebAssembly.instantiate(bytes, imports);
} else {
const instance = await WebAssembly.instantiate(module, imports);
if (instance instanceof WebAssembly.Instance) {
return { instance, module };
} else {
return instance;
}
}
}
async function init(wasmModule) {
const imports = {};
imports.wbg = {};
imports.wbg.__wbg_new_693216e109162396 = function () {
var ret = new Error();
return addHeapObject(ret);
};
imports.wbg.__wbg_stack_0ddaca5d1abfb52f = function (arg0, arg1) {
var ret = getObject(arg1).stack;
var ptr0 = passStringToWasm0(ret, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
var len0 = WASM_VECTOR_LEN;
getInt32Memory0()[arg0 / 4 + 1] = len0;
getInt32Memory0()[arg0 / 4 + 0] = ptr0;
};
imports.wbg.__wbg_error_09919627ac0992f5 = function (arg0, arg1) {
try {
console.error(getStringFromWasm0(arg0, arg1));
} finally {
wasm.__wbindgen_free(arg0, arg1);
}
};
imports.wbg.__wbindgen_object_drop_ref = function (arg0) {
takeObject(arg0);
};
imports.wbg.__wbg_writebyte_81064940ca9059c1 = function (arg0, arg1, arg2) {
write_byte(getObject(arg0), arg1 >>> 0, arg2);
};
imports.wbg.__wbg_readbyte_63aea980ce35d833 = function (arg0, arg1) {
var ret = read_byte(getObject(arg0), arg1 >>> 0);
return ret;
};
imports.wbg.__wbg_getmemorysize_385fa0bd4e2d9ff6 = function (arg0) {
var ret = get_memory_size(getObject(arg0));
return ret;
};
imports.wbg.__wbg_callexport_cb1a6ee1197892bd = function (arg0, arg1, arg2, arg3, arg4, arg5) {
var ret = call_export(getObject(arg1), getStringFromWasm0(arg2, arg3), getStringFromWasm0(arg4, arg5));
var ptr0 = passStringToWasm0(ret, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
var len0 = WASM_VECTOR_LEN;
getInt32Memory0()[arg0 / 4 + 1] = len0;
getInt32Memory0()[arg0 / 4 + 0] = ptr0;
};
imports.wbg.__wbg_alert_607ccf701013ae06 = function (arg0, arg1) {
console.log(getStringFromWasm0(arg0, arg1));
};
const instance = await WebAssembly.instantiate(wasmModule, imports);
wasm = instance.exports;
// strange line from autogenerated code. No idea why it's needed
init.__wbindgen_wasm_module = module;
return wasm;
}
await init(module);
return {
register_module,
call_module,
greet,
};
}
export default init;

View File

@ -0,0 +1,26 @@
{
"compilerOptions": {
"lib": [
"es2015",
"dom"
],
"outDir": "./dist/",
"target": "es5",
"module": "commonjs",
"allowJs": true,
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"declaration": true,
"declarationMap": false,
"sourceMap": true,
},
"exclude": [
"node_modules",
"dist"
],
"include": [
"src"
],
}