@webassemblyjs/helper-module-context
Advanced tools
Comparing version 1.8.1 to 1.8.2
@@ -8,2 +8,3 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
// TODO(sven): add flow in here | ||
import { isSignature, isNumberLiteral } from "@webassemblyjs/ast"; | ||
export function moduleContextFromModuleAST(m) { | ||
@@ -170,14 +171,34 @@ var moduleContext = new ModuleContext(); | ||
value: function importFunction(funcimport) { | ||
// eslint-disable-next-line prefer-const | ||
var _funcimport$signature = funcimport.signature, | ||
args = _funcimport$signature.params, | ||
result = _funcimport$signature.results; | ||
args = args.map(function (arg) { | ||
return arg.valtype; | ||
}); | ||
this.funcs.push({ | ||
args: args, | ||
result: result | ||
}); | ||
if (isSignature(funcimport.signature)) { | ||
// eslint-disable-next-line prefer-const | ||
var _funcimport$signature = funcimport.signature, | ||
args = _funcimport$signature.params, | ||
result = _funcimport$signature.results; | ||
args = args.map(function (arg) { | ||
return arg.valtype; | ||
}); | ||
this.funcs.push({ | ||
args: args, | ||
result: result | ||
}); | ||
} else { | ||
if (!isNumberLiteral(funcimport.signature)) { | ||
throw new Error('isNumberLiteral(funcimport.signature)' + " error: " + (undefined || "unknown")); | ||
} | ||
var typeId = funcimport.signature.value; | ||
if (!this.hasType(typeId)) { | ||
throw new Error('this.hasType(typeId)' + " error: " + (undefined || "unknown")); | ||
} | ||
var signature = this.getType(typeId); | ||
this.funcs.push({ | ||
args: signature.params.map(function (arg) { | ||
return arg.valtype; | ||
}), | ||
result: signature.results | ||
}); | ||
} | ||
if (typeof funcimport.id !== "undefined") { | ||
@@ -184,0 +205,0 @@ // imports are first, we can assume their index in the array |
@@ -9,2 +9,4 @@ "use strict"; | ||
var _ast = require("@webassemblyjs/ast"); | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
@@ -16,3 +18,2 @@ | ||
// TODO(sven): add flow in here | ||
function moduleContextFromModuleAST(m) { | ||
@@ -180,14 +181,34 @@ var moduleContext = new ModuleContext(); | ||
value: function importFunction(funcimport) { | ||
// eslint-disable-next-line prefer-const | ||
var _funcimport$signature = funcimport.signature, | ||
args = _funcimport$signature.params, | ||
result = _funcimport$signature.results; | ||
args = args.map(function (arg) { | ||
return arg.valtype; | ||
}); | ||
this.funcs.push({ | ||
args: args, | ||
result: result | ||
}); | ||
if ((0, _ast.isSignature)(funcimport.signature)) { | ||
// eslint-disable-next-line prefer-const | ||
var _funcimport$signature = funcimport.signature, | ||
args = _funcimport$signature.params, | ||
result = _funcimport$signature.results; | ||
args = args.map(function (arg) { | ||
return arg.valtype; | ||
}); | ||
this.funcs.push({ | ||
args: args, | ||
result: result | ||
}); | ||
} else { | ||
if (!(0, _ast.isNumberLiteral)(funcimport.signature)) { | ||
throw new Error('isNumberLiteral(funcimport.signature)' + " error: " + (undefined || "unknown")); | ||
} | ||
var typeId = funcimport.signature.value; | ||
if (!this.hasType(typeId)) { | ||
throw new Error('this.hasType(typeId)' + " error: " + (undefined || "unknown")); | ||
} | ||
var signature = this.getType(typeId); | ||
this.funcs.push({ | ||
args: signature.params.map(function (arg) { | ||
return arg.valtype; | ||
}), | ||
result: signature.results | ||
}); | ||
} | ||
if (typeof funcimport.id !== "undefined") { | ||
@@ -194,0 +215,0 @@ // imports are first, we can assume their index in the array |
{ | ||
"name": "@webassemblyjs/helper-module-context", | ||
"version": "1.8.1", | ||
"version": "1.8.2", | ||
"description": "", | ||
@@ -15,3 +15,3 @@ "main": "lib/index.js", | ||
"devDependencies": { | ||
"@webassemblyjs/wast-parser": "1.8.1", | ||
"@webassemblyjs/wast-parser": "1.8.2", | ||
"mamacro": "^0.0.3" | ||
@@ -24,3 +24,3 @@ }, | ||
"license": "MIT", | ||
"gitHead": "a2f42245e9b597e3541e0f697253449d60fc4d79" | ||
"gitHead": "02af462b507aa7a24f5d3201178434b181bcdabb" | ||
} |
// TODO(sven): add flow in here | ||
import { isSignature, isNumberLiteral } from "@webassemblyjs/ast"; | ||
import { assert } from "mamacro"; | ||
@@ -138,8 +139,21 @@ | ||
importFunction(funcimport) { | ||
// eslint-disable-next-line prefer-const | ||
let { params: args, results: result } = funcimport.signature; | ||
args = args.map(arg => arg.valtype); | ||
if (isSignature(funcimport.signature)) { | ||
// eslint-disable-next-line prefer-const | ||
let { params: args, results: result } = funcimport.signature; | ||
args = args.map(arg => arg.valtype); | ||
this.funcs.push({ args, result }); | ||
this.funcs.push({ args, result }); | ||
} else { | ||
assert(isNumberLiteral(funcimport.signature)); | ||
const typeId = funcimport.signature.value; | ||
assert(this.hasType(typeId)); | ||
const signature = this.getType(typeId); | ||
this.funcs.push({ | ||
args: signature.params.map(arg => arg.valtype), | ||
result: signature.results | ||
}); | ||
} | ||
if (typeof funcimport.id !== "undefined") { | ||
@@ -146,0 +160,0 @@ // imports are first, we can assume their index in the array |
@@ -31,3 +31,71 @@ const { assert } = require("chai"); | ||
}); | ||
it("should retrive the type of implemented functions", () => { | ||
const context = contextFromWast(` | ||
(module | ||
(func (param i32) (result i64)) | ||
(func (param i64) (result i32)) | ||
(func (result i64)) | ||
(func) | ||
) | ||
`); | ||
assert.deepEqual(context.getFunction(0), { | ||
args: ["i32"], | ||
result: ["i64"] | ||
}); | ||
assert.deepEqual(context.getFunction(1), { | ||
args: ["i64"], | ||
result: ["i32"] | ||
}); | ||
assert.deepEqual(context.getFunction(2), { args: [], result: ["i64"] }); | ||
assert.deepEqual(context.getFunction(3), { args: [], result: [] }); | ||
}); | ||
it("should retrive the type of imported functions", () => { | ||
const context = contextFromWast(` | ||
(module | ||
(import "a" "a" (func (param i32) (result i32))) | ||
(import "a" "b" (func (result i64))) | ||
(import "a" "c" (func)) | ||
(func (result f32)) | ||
) | ||
`); | ||
assert.deepEqual(context.getFunction(0), { | ||
args: ["i32"], | ||
result: ["i32"] | ||
}); | ||
assert.deepEqual(context.getFunction(1), { | ||
args: [], | ||
result: ["i64"] | ||
}); | ||
assert.deepEqual(context.getFunction(2), { args: [], result: [] }); | ||
assert.deepEqual(context.getFunction(3), { args: [], result: ["f32"] }); | ||
}); | ||
it("should retrive the type of functions with type ref", () => { | ||
const context = contextFromWast(` | ||
(module | ||
(type (func (param i32) (result i32))) | ||
(type (func (result i64))) | ||
(type (func)) | ||
(import "a" "a" (func (type 0))) | ||
(import "a" "b" (func (type 1))) | ||
(func (type 2)) | ||
) | ||
`); | ||
assert.deepEqual(context.getFunction(0), { | ||
args: ["i32"], | ||
result: ["i32"] | ||
}); | ||
assert.deepEqual(context.getFunction(1), { | ||
args: [], | ||
result: ["i64"] | ||
}); | ||
assert.deepEqual(context.getFunction(2), { args: [], result: [] }); | ||
}); | ||
}); | ||
}); |
29722
1002