From c147e98a55a924b8090059aefabddd0e3fc3e7b1 Mon Sep 17 00:00:00 2001 From: dcode Date: Mon, 18 Mar 2019 13:45:10 +0100 Subject: [PATCH] capabilities to detect half/full runtime header --- lib/loader/index.js | 5 +++-- src/compiler.ts | 22 +++++++++++++++++++++- tests/compiler/exports.optimized.wat | 2 +- tests/compiler/exports.untouched.wat | 2 +- tests/compiler/std/runtime.optimized.wat | 2 ++ tests/compiler/std/runtime.untouched.wat | 2 ++ 6 files changed, 30 insertions(+), 5 deletions(-) diff --git a/lib/loader/index.js b/lib/loader/index.js index 7f79027b..5e58a1bf 100644 --- a/lib/loader/index.js +++ b/lib/loader/index.js @@ -54,7 +54,8 @@ function postInstantiate(baseModule, instance) { var memory_fill = rawExports["memory.fill"]; var memory_free = rawExports["memory.free"]; var table = rawExports.table; - var setargc = rawExports._setargc || function() {}; + var capabilities = rawExports[".capabilities"] || 0; + var setargc = rawExports[".setargc"] || function() {}; // Provide views for all sorts of basic values var buffer, I8, U8, I16, U16, I32, U32, F32, F64, I64, U64; @@ -245,7 +246,7 @@ exports.instantiateStreaming = instantiateStreaming; /** Demangles an AssemblyScript module's exports to a friendly object structure. */ function demangle(exports, baseModule) { var module = baseModule ? Object.create(baseModule) : {}; - var setargc = exports._setargc || function() {}; + var setargc = exports[".setargc"] || function() {}; function hasOwnProperty(elem, prop) { return Object.prototype.hasOwnProperty.call(elem, prop); } diff --git a/src/compiler.ts b/src/compiler.ts index 1de1b5f7..a110de0c 100644 --- a/src/compiler.ts +++ b/src/compiler.ts @@ -252,6 +252,16 @@ export const enum Feature { THREADS = 1 << 4 // see: https://github.com/WebAssembly/threads } +/** Indicates module capabilities. */ +export const enum Capability { + /** No specific capabilities. */ + NONE = 0, + /** Uses WebAssembly with 64-bit pointers. */ + WASM64 = 1 << 0, + /** Garbage collector is present (full runtime header). */ + GC = 1 << 1 +} + /** Indicates the desired kind of a conversion. */ export const enum ConversionKind { /** No conversion. */ @@ -440,6 +450,14 @@ export class Compiler extends DiagnosticEmitter { // set up gc if (this.needsIterateRoots) compileIterateRoots(this); + // expose module capabilities + var capabilities = Capability.NONE; + if (program.options.isWasm64) capabilities |= Capability.WASM64; + if (program.gcImplemented) capabilities |= Capability.GC; + if (capabilities != 0) { + module.addGlobal(CompilerSymbols.capabilities, NativeType.I32, false, module.createI32(capabilities)); + module.addGlobalExport(CompilerSymbols.capabilities, ".capabilities"); + } return module; } @@ -5758,7 +5776,7 @@ export class Compiler extends DiagnosticEmitter { module.createGetLocal(0, NativeType.I32) ) ); - module.addFunctionExport(internalName, "_setargc"); + module.addFunctionExport(internalName, ".setargc"); } return internalName; } @@ -8088,4 +8106,6 @@ namespace CompilerSymbols { export const argc = "~lib/argc"; /** Argument count setter. Exported for use by host calls. */ export const setargc = "~lib/setargc"; + /** Module capabilities. Exported for evaluation by the host. */ + export const capabilities = "~lib/capabilities"; } diff --git a/tests/compiler/exports.optimized.wat b/tests/compiler/exports.optimized.wat index e4056d28..35b75ab2 100644 --- a/tests/compiler/exports.optimized.wat +++ b/tests/compiler/exports.optimized.wat @@ -24,7 +24,7 @@ (export "memory" (memory $0)) (export "table" (table $0)) (export "add" (func $exports/add)) - (export "_setargc" (func $~lib/setargc)) + (export ".setargc" (func $~lib/setargc)) (export "subOpt" (func $exports/subOpt|trampoline)) (export "math.sub" (func $exports/subOpt)) (export "Animal.CAT" (global $exports/Animal.CAT)) diff --git a/tests/compiler/exports.untouched.wat b/tests/compiler/exports.untouched.wat index 5ebb60c9..9c3e2d56 100644 --- a/tests/compiler/exports.untouched.wat +++ b/tests/compiler/exports.untouched.wat @@ -29,7 +29,7 @@ (export "memory" (memory $0)) (export "table" (table $0)) (export "add" (func $exports/add)) - (export "_setargc" (func $~lib/setargc)) + (export ".setargc" (func $~lib/setargc)) (export "subOpt" (func $exports/subOpt|trampoline)) (export "math.sub" (func $exports/math.sub)) (export "Animal.CAT" (global $exports/Animal.CAT)) diff --git a/tests/compiler/std/runtime.optimized.wat b/tests/compiler/std/runtime.optimized.wat index 45871616..adaa3012 100644 --- a/tests/compiler/std/runtime.optimized.wat +++ b/tests/compiler/std/runtime.optimized.wat @@ -38,8 +38,10 @@ (global $std/runtime/ref4 (mut i32) (i32.const 0)) (global $std/runtime/header4 (mut i32) (i32.const 0)) (global $std/runtime/ref5 (mut i32) (i32.const 0)) + (global $~lib/capabilities i32 (i32.const 2)) (export "memory" (memory $0)) (export "table" (table $0)) + (export ".capabilities" (global $~lib/capabilities)) (start $start) (func $~lib/allocator/tlsf/Root#setSLMap (; 2 ;) (type $FUNCSIG$viii) (param $0 i32) (param $1 i32) (param $2 i32) local.get $1 diff --git a/tests/compiler/std/runtime.untouched.wat b/tests/compiler/std/runtime.untouched.wat index fc1f1ed7..a91a32ae 100644 --- a/tests/compiler/std/runtime.untouched.wat +++ b/tests/compiler/std/runtime.untouched.wat @@ -55,8 +55,10 @@ (global $std/runtime/header4 (mut i32) (i32.const 0)) (global $std/runtime/ref5 (mut i32) (i32.const 0)) (global $~lib/memory/HEAP_BASE i32 (i32.const 264)) + (global $~lib/capabilities i32 (i32.const 2)) (export "memory" (memory $0)) (export "table" (table $0)) + (export ".capabilities" (global $~lib/capabilities)) (start $start) (func $start:~lib/allocator/tlsf (; 2 ;) (type $FUNCSIG$v) i32.const 1