Skip to content
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,18 @@ module.exports = {
"es6": true
},
files: [
"**/*.js"
"**/*.js",
"**/*.mjs",
"**/*.cjs"
],
rules: {
// Node's support for ESM is still not great, but this rule is likely
// to become activated once compatibility doesn't suck anymore.
"@typescript-eslint/no-var-requires": "off",

// There are no types in JavaScript
"@typescript-eslint/explicit-module-boundary-types": "off",

// Enforcing to remove function parameters on stubs makes code less
// maintainable, so we instead allow unused function parameters.
"no-unused-vars": [
Expand Down
1 change: 0 additions & 1 deletion lib/loader/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ export interface ResultObject {
instance: WebAssembly.Instance;
}


/** WebAssembly imports with an optional env object and two levels of nesting. */
export type Imports = {
[key: string]: Record<string,unknown>;
Expand Down
34 changes: 10 additions & 24 deletions lib/loader/index.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,19 @@
"use strict";

// Runtime header offsets
const ID_OFFSET = -8;
const SIZE_OFFSET = -4;

// Runtime ids
const ARRAYBUFFER_ID = 0;
const STRING_ID = 1;
// const ARRAYBUFFERVIEW_ID = 2;

// Runtime type information
const ARRAYBUFFERVIEW = 1 << 0;
const ARRAY = 1 << 1;
const STATICARRAY = 1 << 2;
// const SET = 1 << 3;
// const MAP = 1 << 4;
const VAL_ALIGN_OFFSET = 6;
// const VAL_ALIGN = 1 << VAL_ALIGN_OFFSET;
const VAL_SIGNED = 1 << 11;
const VAL_FLOAT = 1 << 12;
// const VAL_NULLABLE = 1 << 13;
const VAL_MANAGED = 1 << 14;
// const KEY_ALIGN_OFFSET = 15;
// const KEY_ALIGN = 1 << KEY_ALIGN_OFFSET;
// const KEY_SIGNED = 1 << 20;
// const KEY_FLOAT = 1 << 21;
// const KEY_NULLABLE = 1 << 22;
// const KEY_MANAGED = 1 << 23;

// Array(BufferView) layout
const ARRAYBUFFERVIEW_BUFFER_OFFSET = 0;
Expand Down Expand Up @@ -299,7 +286,7 @@ function isModule(src) {
}

/** Asynchronously instantiates an AssemblyScript module from anything that can be instantiated. */
async function instantiate(source, imports = {}) {
export async function instantiate(source, imports = {}) {
if (isResponse(source = await source)) return instantiateStreaming(source, imports);
const module = isModule(source) ? source : await WebAssembly.compile(source);
const extended = preInstantiate(imports);
Expand All @@ -308,21 +295,17 @@ async function instantiate(source, imports = {}) {
return { module, instance, exports };
}

exports.instantiate = instantiate;

/** Synchronously instantiates an AssemblyScript module from a WebAssembly.Module or binary buffer. */
function instantiateSync(source, imports = {}) {
export function instantiateSync(source, imports = {}) {
const module = isModule(source) ? source : new WebAssembly.Module(source);
const extended = preInstantiate(imports);
const instance = new WebAssembly.Instance(module, imports);
const exports = postInstantiate(extended, instance);
return { module, instance, exports };
}

exports.instantiateSync = instantiateSync;

/** Asynchronously instantiates an AssemblyScript module from a response, i.e. as obtained by `fetch`. */
async function instantiateStreaming(source, imports = {}) {
export async function instantiateStreaming(source, imports = {}) {
if (!WebAssembly.instantiateStreaming) {
return instantiate(
isResponse(source = await source)
Expand All @@ -337,10 +320,8 @@ async function instantiateStreaming(source, imports = {}) {
return { ...result, exports };
}

exports.instantiateStreaming = instantiateStreaming;

/** Demangles an AssemblyScript module's exports to a friendly object structure. */
function demangle(exports, extendedExports = {}) {
export function demangle(exports, extendedExports = {}) {
extendedExports = Object.create(extendedExports);
const setArgumentsLength = exports["__argumentsLength"]
? length => { exports["__argumentsLength"].value = length; }
Expand Down Expand Up @@ -424,4 +405,9 @@ function demangle(exports, extendedExports = {}) {
return extendedExports;
}

exports.demangle = demangle;
export default {
instantiate,
instantiateSync,
instantiateStreaming,
demangle
};
22 changes: 17 additions & 5 deletions lib/loader/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,30 @@
"bugs": {
"url": "https://github.com/AssemblyScript/assemblyscript/issues"
},
"main": "index.js",
"types": "index.d.ts",
"type": "module",
"main": "./umd/index.js",
"exports": {
".": {
"require": "./umd/index.js",
"import": "./index.js"
}
},
"types": "./index.d.ts",
"module": "./index.js",
"scripts": {
"asbuild": "npm run asbuild:default && npm run asbuild:legacy",
"asbuild:default": "node ../../bin/asc tests/assembly/index.ts -b tests/build/default.wasm",
"asbuild:legacy": "node ../../bin/asc tests/assembly/index.ts --disable mutable-globals -b tests/build/legacy.wasm",
"test": "node tests"
"test": "node tests",
"build": "npx esm2umd loader index.js > umd/index.js",
"prepublishOnly": "npm run build"
},
"files": [
"index.d.ts",
"index.js",
"index.d.ts",
"package.json",
"README.md"
"README.md",
"umd/index.js",
"umd/package.json"
]
}
17 changes: 10 additions & 7 deletions lib/loader/tests/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
var fs = require("fs");
var assert = require("assert");
var inspect = require("util").inspect;
import fs from "fs";
import assert from "assert";
import { inspect } from "util";
import { dirname } from 'path';
import { fileURLToPath } from 'url';

var loader = require("..");
import loader from "../index.js";

const __dirname = dirname(fileURLToPath(import.meta.url));

test("default.wasm");
test("legacy.wasm");
Expand All @@ -11,9 +15,8 @@ testInstantiate("default.wasm");
testInstantiate("legacy.wasm");

function test(file) {
var buffer = fs.readFileSync(__dirname + "/build/" + file);
var result = loader.instantiateSync(buffer, {});
const exports = result.exports;
const buffer = fs.readFileSync(__dirname + "/build/" + file);
const exports = loader.instantiateSync(buffer, {}).exports;

console.log(inspect(exports, true, 100, true));

Expand Down
Loading