| // Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file |
| // for details. All rights reserved. Use of this source code is governed by a |
| // BSD-style license that can be found in the LICENSE file. |
| |
| const jsRuntimeBlobPart1 = r''' |
| // Returns whether the `js-string` built-in is supported. |
| function detectJsStringBuiltins() { |
| let bytes = [ |
| 0, 97, 115, 109, 1, 0, 0, 0, 1, 4, 1, 96, 0, |
| 0, 2, 23, 1, 14, 119, 97, 115, 109, 58, 106, 115, 45, |
| 115, 116, 114, 105, 110, 103, 4, 99, 97, 115, 116, 0, 0 |
| ]; |
| return WebAssembly.validate( |
| new Uint8Array(bytes), {builtins: ['js-string']}); |
| } |
| |
| // Compiles a dart2wasm-generated wasm modules from `source` which is then |
| // instantiatable via the `instantiate` method. |
| // |
| // `source` needs to be a `Response` object (or promise thereof) e.g. created |
| // via the `fetch()` JS API. |
| export async function compileStreaming(source) { |
| const module = await WebAssembly.compileStreaming( |
| source, |
| detectJsStringBuiltins() ? {builtins: ['js-string']} : {} |
| ); |
| return new CompiledApp(module); |
| } |
| |
| // Compiles a dart2wasm-generated wasm modules from `bytes` which is then |
| // instantiatable via the `instantiate` method. |
| export async function compile(bytes) { |
| const module = await WebAssembly.compile( |
| bytes, |
| detectJsStringBuiltins() ? {builtins: ['js-string']} : {} |
| ); |
| return new CompiledApp(module); |
| } |
| |
| // DEPRECATED: Please use `compile` or `compileStreaming` to get a compiled app, |
| // use `instantiate` method to get an instantiated app and then call |
| // `invokeMain` to invoke the main function. |
| export async function instantiate(modulePromise, importObjectPromise) { |
| var moduleOrCompiledApp = await modulePromise; |
| if (!(moduleOrCompiledApp instanceof CompiledApp)) { |
| moduleOrCompiledApp = new CompiledApp(moduleOrCompiledApp); |
| } |
| const instantiatedApp = await moduleOrCompiledApp.instantiate(await importObjectPromise); |
| return instantiatedApp.instantiatedModule; |
| } |
| |
| // DEPRECATED: Please use `compile` or `compileStreaming` to get a compiled app, |
| // use `instantiate` method to get an instantiated app and then call |
| // `invokeMain` to invoke the main function. |
| export const invoke = (moduleInstance, ...args) => { |
| moduleInstance.exports.$invokeMain(args); |
| } |
| |
| class CompiledApp { |
| constructor(module) { |
| this.module = module; |
| } |
| |
| async instantiate(additionalImports) { |
| let dartInstance; |
| |
| // Prints to the console |
| function printToConsole(value) { |
| if (typeof dartPrint == "function") { |
| dartPrint(value); |
| return; |
| } |
| if (typeof console == "object" && typeof console.log != "undefined") { |
| console.log(value); |
| return; |
| } |
| if (typeof print == "function") { |
| print(value); |
| return; |
| } |
| |
| throw "Unable to print message: " + js; |
| } |
| |
| // Converts a Dart List to a JS array. Any Dart objects will be converted, but |
| // this will be cheap for JSValues. |
| function arrayFromDartList(constructor, list) { |
| const exports = dartInstance.exports; |
| const read = exports.$listRead; |
| const length = exports.$listLength(list); |
| const array = new constructor(length); |
| for (let i = 0; i < length; i++) { |
| array[i] = read(list, i); |
| } |
| return array; |
| } |
| |
| // A special symbol attached to functions that wrap Dart functions. |
| const jsWrappedDartFunctionSymbol = Symbol("JSWrappedDartFunction"); |
| |
| function finalizeWrapper(dartFunction, wrapped) { |
| wrapped.dartFunction = dartFunction; |
| wrapped[jsWrappedDartFunctionSymbol] = true; |
| return wrapped; |
| } |
| |
| // Imports |
| const dart2wasm = { |
| '''; |
| |
| // We break inside the 'dart2wasm' object to enable injection of methods. We |
| // could use interpolation, but then we'd have to escape characters. |
| const jsRuntimeBlobPart2 = r''' |
| }; |
| |
| const baseImports = { |
| dart2wasm: dart2wasm, |
| '''; |
| |
| // We break inside of `baseImports` to inject internalized strings. |
| const jsRuntimeBlobPart3 = r''' |
| Math: Math, |
| Date: Date, |
| Object: Object, |
| Array: Array, |
| Reflect: Reflect, |
| }; |
| |
| const jsStringPolyfill = { |
| "charCodeAt": (s, i) => s.charCodeAt(i), |
| "compare": (s1, s2) => { |
| if (s1 < s2) return -1; |
| if (s1 > s2) return 1; |
| return 0; |
| }, |
| "concat": (s1, s2) => s1 + s2, |
| "equals": (s1, s2) => s1 === s2, |
| "fromCharCode": (i) => String.fromCharCode(i), |
| "length": (s) => s.length, |
| "substring": (s, a, b) => s.substring(a, b), |
| }; |
| |
| dartInstance = await WebAssembly.instantiate(this.module, { |
| ...baseImports, |
| ...additionalImports, |
| "wasm:js-string": jsStringPolyfill, |
| }); |
| |
| return new InstantiatedApp(this, dartInstance); |
| } |
| } |
| |
| class InstantiatedApp { |
| constructor(compiledApp, instantiatedModule) { |
| this.compiledApp = compiledApp; |
| this.instantiatedModule = instantiatedModule; |
| } |
| |
| // Call the main function with the given arguments. |
| invokeMain(...args) { |
| this.instantiatedModule.exports.$invokeMain(args); |
| } |
| } |
| '''; |