Skip to content

Commit b141a95

Browse files
authored
Merge pull request #126 from WebAssembly/esm-shared-import-reading
[js-api] Share import reading between the JS API and ESM integration
2 parents 38aef62 + 3207bbb commit b141a95

1 file changed

Lines changed: 69 additions & 99 deletions

File tree

‎document/js-api/index.bs‎

Lines changed: 69 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -512,16 +512,7 @@ To <dfn>instantiate imported strings</dfn> with module |module| and |importedStr
512512
<div algorithm="read-the-imports">
513513
To <dfn>read the imports</dfn> from a WebAssembly module |module| from imports object |importObject|, enabled builtins |builtinSetNames|, and |importedStringModule|, perform the following steps:
514514
1. If |module|.[=imports=] [=list/is empty|is not empty=], and |importObject| is undefined, throw a {{TypeError}} exception.
515-
1. Let |builtinOrStringImports| be the ordered map « ».
516-
1. [=list/iterate|For each=] |builtinSetName| of |builtinSetNames|,
517-
1. Let |builtinSetQualifiedName| be |builtinSetName| prefixed with "wasm:"
518-
1. Assert: |builtinOrStringImports| does not contain |builtinSetQualifiedName|
519-
1. If |builtinSetName| does not refer to a builtin set, then [=iteration/continue=].
520-
1. Let |exportsObject| be the result of [=instantiate a builtin set=] with |builtinSetName|
521-
1. [=map/set|Set=] |builtinOrStringImports|[|builtinSetQualifiedName|] to |exportsObject|
522-
1. If |importedStringModule| is not null,
523-
1. Let |exportsObject| be the result of [=instantiate imported strings=] with |module| and |importedStringModule|
524-
1. [=map/set|Set=] |builtinOrStringImports|[|importedStringModule|] to |exportsObject|
515+
1. Let |builtinOrStringImports| be the result of [=instantiate the builtin and string imports=] of |module| with |builtinSetNames| and |importedStringModule|.
525516
1. Let |imports| be « ».
526517
1. [=list/iterate|For each=] (|moduleName|, |componentName|, |externtype|) of [=module_imports=](|module|),
527518
1. If |builtinOrStringImports| [=map/exist|contains=] |moduleName|,
@@ -532,53 +523,73 @@ To <dfn>instantiate imported strings</dfn> with module |module| and |importedStr
532523
1. Let |o| be [=?=] [$Get$](|importObject|, |moduleName|).
533524
1. If |o| [=is not an Object=], throw a {{TypeError}} exception.
534525
1. Let |v| be [=?=] [$Get$](|o|, |componentName|).
535-
1. If |externtype| is of the form [=external-type/func=] |functype|,
536-
1. If [$IsCallable$](|v|) is false, throw a {{LinkError}} exception.
537-
1. If |v| has a \[[FunctionAddress]] internal slot, and therefore is an [=Exported Function=],
538-
1. Let |funcaddr| be the value of |v|'s \[[FunctionAddress]] internal slot.
539-
1. Otherwise,
540-
1. [=Create a host function=] from |v| and |functype|, and let |funcaddr| be the result.
541-
1. Let |index| be the number of external functions in |imports|. This value |index| is known as the <dfn>index of the host function</dfn> |funcaddr|.
542-
1. Let |externfunc| be the [=external value=] [=external value|func=] |funcaddr|.
543-
1. [=list/Append=] |externfunc| to |imports|.
544-
1. If |externtype| is of the form [=external-type/global=] <var ignore>mut</var> |valtype|,
545-
1. If |v| [=implements=] {{Global}},
546-
1. Let |globaladdr| be |v|.\[[Global]].
547-
1. Otherwise,
548-
1. If |valtype| is [=i64=] and |v| [=is not a BigInt=],
549-
1. Throw a {{LinkError}} exception.
550-
1. If |valtype| is one of [=i32=], [=f32=] or [=f64=] and |v| [=is not a Number=],
551-
1. Throw a {{LinkError}} exception.
552-
1. If |valtype| is [=v128=],
553-
1. Throw a {{LinkError}} exception.
554-
1. Let |value| be [=ToWebAssemblyValue=](|v|, |valtype|). If this operation throws a {{TypeError}}, catch it, and throw a {{LinkError}} exception.
555-
1. Let |store| be the [=surrounding agent=]'s [=associated store=].
556-
1. Let (|store|, |globaladdr|) be [=global_alloc=](|store|, [=const=] |valtype|, |value|).
557-
1. Set the [=surrounding agent=]'s [=associated store=] to |store|.
558-
1. Let |externglobal| be [=external value|global=] |globaladdr|.
559-
1. [=list/Append=] |externglobal| to |imports|.
560-
1. If |externtype| is of the form [=external-type/mem=] <var ignore>memtype</var>,
561-
1. If |v| does not [=implement=] {{Memory}}, throw a {{LinkError}} exception.
562-
1. Let |externmem| be the [=external value=] [=external value|mem=] |v|.\[[Memory]].
563-
1. [=list/Append=] |externmem| to |imports|.
564-
1. If |externtype| is of the form [=external-type/table=] <var ignore>tabletype</var>,
565-
1. If |v| does not [=implement=] {{Table}}, throw a {{LinkError}} exception.
566-
1. Let |tableaddr| be |v|.\[[Table]].
567-
1. Let |externtable| be the [=external value=] [=external value|table=] |tableaddr|.
568-
1. [=list/Append=] |externtable| to |imports|.
569-
1. If |externtype| is of the form [=external-type/tag=] |attribute| <var ignore>functype</var>,
570-
1. Assert: |attribute| is [=tagtype/attribute/exception=].
571-
1. If |v| does not [=implement=] {{Tag}}, throw a {{LinkError}} exception.
572-
1. Let |tagaddr| be |v|.\[[Address]].
573-
1. Let |externtag| be the [=external value=] [=external value/tag=] |tagaddr|.
574-
1. [=list/Append=] |externtag| to |imports|.
526+
1. [=Read an import value=] |v| of type |externtype| into |imports|.
575527
1. Return |imports|.
576528

577529
Note: This algorithm only verifies the right kind of JavaScript values are passed.
578530
The verification of WebAssembly type requirements is deferred to the
579531
"[=instantiate the core of a WebAssembly module=]" algorithm.
580532
</div>
581533

534+
<div algorithm>
535+
To <dfn>instantiate the builtin and string imports</dfn> of a WebAssembly module |module| with enabled builtins |builtinSetNames| and |importedStringModule|, perform the following steps:
536+
1. Let |builtinOrStringImports| be the ordered map « ».
537+
1. [=list/iterate|For each=] |builtinSetName| of |builtinSetNames|,
538+
1. Let |builtinSetQualifiedName| be |builtinSetName| prefixed with "wasm:"
539+
1. Assert: |builtinOrStringImports| does not contain |builtinSetQualifiedName|
540+
1. If |builtinSetName| does not refer to a builtin set, then [=iteration/continue=].
541+
1. Let |exportsObject| be the result of [=instantiate a builtin set=] with |builtinSetName|
542+
1. [=map/set|Set=] |builtinOrStringImports|[|builtinSetQualifiedName|] to |exportsObject|
543+
1. If |importedStringModule| is not null,
544+
1. Let |exportsObject| be the result of [=instantiate imported strings=] with |module| and |importedStringModule|
545+
1. [=map/set|Set=] |builtinOrStringImports|[|importedStringModule|] to |exportsObject|
546+
1. Return |builtinOrStringImports|.
547+
</div>
548+
549+
<div algorithm="read an import value">
550+
To <dfn>read an import value</dfn> |v| of type |externtype| into imports list |imports|, perform the following steps:
551+
1. If |externtype| is of the form [=external-type/func=] |functype|,
552+
1. If [$IsCallable$](|v|) is false, throw a {{LinkError}} exception.
553+
1. If |v| has a \[[FunctionAddress]] internal slot, and therefore is an [=Exported Function=],
554+
1. Let |funcaddr| be the value of |v|'s \[[FunctionAddress]] internal slot.
555+
1. Otherwise,
556+
1. [=Create a host function=] from |v| and |functype|, and let |funcaddr| be the result.
557+
1. Let |index| be the number of external functions in |imports|. This value |index| is known as the <dfn>index of the host function</dfn> |funcaddr|.
558+
1. Let |externfunc| be the [=external value=] [=external value|func=] |funcaddr|.
559+
1. [=list/Append=] |externfunc| to |imports|.
560+
1. If |externtype| is of the form [=external-type/global=] <var ignore>mut</var> |valtype|,
561+
1. If |v| [=implements=] {{Global}},
562+
1. Let |globaladdr| be |v|.\[[Global]].
563+
1. Otherwise,
564+
1. If |valtype| is [=i64=] and |v| [=is not a BigInt=],
565+
1. Throw a {{LinkError}} exception.
566+
1. If |valtype| is one of [=i32=], [=f32=] or [=f64=] and |v| [=is not a Number=],
567+
1. Throw a {{LinkError}} exception.
568+
1. If |valtype| is [=v128=],
569+
1. Throw a {{LinkError}} exception.
570+
1. Let |value| be [=ToWebAssemblyValue=](|v|, |valtype|). If this operation throws a {{TypeError}}, catch it, and throw a {{LinkError}} exception.
571+
1. Let |store| be the [=surrounding agent=]'s [=associated store=].
572+
1. Let (|store|, |globaladdr|) be [=global_alloc=](|store|, [=const=] |valtype|, |value|).
573+
1. Set the [=surrounding agent=]'s [=associated store=] to |store|.
574+
1. Let |externglobal| be [=external value|global=] |globaladdr|.
575+
1. [=list/Append=] |externglobal| to |imports|.
576+
1. If |externtype| is of the form [=external-type/mem=] <var ignore>memtype</var>,
577+
1. If |v| does not [=implement=] {{Memory}}, throw a {{LinkError}} exception.
578+
1. Let |externmem| be the [=external value=] [=external value|mem=] |v|.\[[Memory]].
579+
1. [=list/Append=] |externmem| to |imports|.
580+
1. If |externtype| is of the form [=external-type/table=] <var ignore>tabletype</var>,
581+
1. If |v| does not [=implement=] {{Table}}, throw a {{LinkError}} exception.
582+
1. Let |tableaddr| be |v|.\[[Table]].
583+
1. Let |externtable| be the [=external value=] [=external value|table=] |tableaddr|.
584+
1. [=list/Append=] |externtable| to |imports|.
585+
1. If |externtype| is of the form [=external-type/tag=] |attribute| <var ignore>functype</var>,
586+
1. Assert: |attribute| is [=tagtype/attribute/exception=].
587+
1. If |v| does not [=implement=] {{Tag}}, throw a {{LinkError}} exception.
588+
1. Let |tagaddr| be |v|.\[[Address]].
589+
1. Let |externtag| be the [=external value=] [=external value/tag=] |tagaddr|.
590+
1. [=list/Append=] |externtag| to |imports|.
591+
</div>
592+
582593
<div algorithm>
583594
To <dfn>create an exports object</dfn> from a WebAssembly module |module| and instance |instance|, perform the following steps:
584595
1. Let |exportsObject| be [=!=] [$OrdinaryObjectCreate$](null).
@@ -2320,6 +2331,7 @@ To <dfn export>parse a WebAssembly module</dfn> given a <a>byte sequence</a> |by
23202331
1. If |module| is [=error=], throw a {{CompileError}} exception.
23212332
1. Let |builtinSetNames| be « "js-string" ».
23222333
1. Let |importedStringModule| be "wasm:js/string-constants".
2334+
1. If [=validate builtins and imported string for a WebAssembly module|validating builtins and imported strings=] for |module| with |builtinSetNames| and |importedStringModule| is false, throw a {{CompileError}} exception.
23232335
1. [=Construct a WebAssembly module object=] from |module|, |bytes|, |builtinSetNames| and |importedStringModule|, and let |module| be the result.
23242336
1. Let |requestedModules| be a set.
23252337
1. For each (|moduleName|, |name|, <var ignore>type</var>) of the [=external imports=] of |module|,
@@ -2417,25 +2429,14 @@ WebAssembly Module Records have the following methods:
24172429
1. Let |module| be |moduleObject|.\[[Module]].
24182430
1. Let |builtinSetNames| be |moduleObject|.\[[BuiltinSets]].
24192431
1. Let |importedStringModule| be |moduleObject|.\[[ImportedStringModule]].
2432+
1. Let |builtinOrStringImports| be the result of [=instantiate the builtin and string imports=] of |module| with |builtinSetNames| and |importedStringModule|.
24202433
1. Let |imports| be « ».
24212434
1. [=list/iterate|For each=] (|importedModuleName|, |name|, |importtype|) of [=module_imports=](|module|),
2422-
1. If |importedStringModule| is not null and |importedModuleName| equals |importedStringModule|,
2423-
1. Assert: |importtype| is of the form [=global=] [=const=] |valtype|.
2424-
1. Let |store| be the [=surrounding agent=]'s [=associated store=].
2425-
1. Let |value| be [=?=] [=ToWebAssemblyValue=](|name|, |valtype|).
2426-
1. Let (|store|, |globaladdr|) be [=global_alloc=](|store|, [=const=] |valtype|, |value|).
2427-
1. Set the [=surrounding agent=]'s [=associated store=] to |store|.
2428-
1. Let |externglobal| be [=external value|global=] |globaladdr|.
2429-
1. [=list/Append=] |externglobal| to |imports|.
2430-
1. [=iteration/continue=].
2431-
1. If |builtinSetNames| is not null,
2432-
1. Let |maybeBuiltin| be the result of [=find a builtin|finding a builtin=] for (|importedModuleName|, |name|, |importtype|) and |builtinSetNames|.
2433-
1. If |maybeBuiltin| is not null,
2434-
1. Let |funcType| be |maybeBuiltin|[1][1].
2435-
1. Let |steps| be |maybeBuiltin|[1][2].
2436-
1. Let |funcaddr| be the result of [=create a builtin function=] with |funcType| and |steps|.
2437-
1. Let |externfunc| be the [=external value=] [=external value|func=] |funcaddr|.
2438-
1. [=list/Append=] |externfunc| to |imports|.
2435+
1. If |builtinOrStringImports| [=map/exist|contains=] |importedModuleName|,
2436+
1. Let |o| be |builtinOrStringImports|[|importedModuleName|].
2437+
1. If [$HasProperty$](|o|, |name|) is true,
2438+
1. Let |v| be [=!=] [$Get$](|o|, |name|).
2439+
1. [=Read an import value=] |v| of type |importtype| into |imports|.
24392440
1. [=iteration/continue=].
24402441
1. Let |importedModule| be [$GetImportedModule$](|record|, |importedModuleName|).
24412442
1. Let |resolution| be |importedModule|.ResolveExport(|name|).
@@ -2457,38 +2458,7 @@ WebAssembly Module Records have the following methods:
24572458
1. Otherwise,
24582459
1. Let |env| be |resolvedModule|.\[[Environment]].
24592460
1. Let |v| be [=?=] |env|.GetBindingValue(|resolvedName|, true).
2460-
1. If |importtype| is of the form [=func=] |functype|,
2461-
1. If [$IsCallable$](|v|) is false, throw a {{LinkError}} exception.
2462-
1. If |v| has a \[[FunctionAddress]] internal slot, and therefore is an [=Exported Function=],
2463-
1. Let |funcaddr| be the value of |v|'s \[[FunctionAddress]] internal slot.
2464-
1. Otherwise,
2465-
1. [=Create a host function=] from |v| and |functype|, and let |funcaddr| be the result.
2466-
1. Let <var ignore>index</var> be the number of external functions in |imports|, defining the [=index of the host function=] |funcaddr|.
2467-
1. Let |externfunc| be the [=external value=] [=external value|func=] |funcaddr|.
2468-
1. [=list/Append=] |externfunc| to |imports|.
2469-
1. If |importtype| is of the form [=global=] |mut| |valtype|,
2470-
1. Let |store| be the [=surrounding agent=]'s [=associated store=].
2471-
1. If |v| [=implements=] {{Global}},
2472-
1. Let |globaladdr| be |v|.\[[Global]].
2473-
1. Let |targetmut| <var ignore>valuetype</var> be [=global_type=](|store|, |globaladdr|).
2474-
1. If |mut| is [=const=] and |targetmut| is [=var=], throw a {{LinkError}} exception.
2475-
1. Otherwise,
2476-
1. If |valtype| is [=v128=], throw a {{LinkError}} exception.
2477-
1. If |mut| is [=var=], throw a {{LinkError}} exception.
2478-
1. Let |value| be [=?=] [=ToWebAssemblyValue=](|v|, |valtype|).
2479-
1. Let (|store|, |globaladdr|) be [=global_alloc=](|store|, |mut| |valtype|, |value|).
2480-
1. Set the [=surrounding agent=]'s [=associated store=] to |store|.
2481-
1. Let |externglobal| be [=external value|global=] |globaladdr|.
2482-
1. [=list/Append=] |externglobal| to |imports|.
2483-
1. If |importtype| is of the form [=mem=] <var ignore>memtype</var>,
2484-
1. If |v| does not [=implement=] {{Memory}}, throw a {{LinkError}} exception.
2485-
1. Let |externmem| be the [=external value=] [=external value|mem=] |v|.\[[Memory]].
2486-
1. [=list/Append=] |externmem| to |imports|.
2487-
1. If |importtype| is of the form [=table=] <var ignore>tabletype</var>,
2488-
1. If |v| does not [=implement=] {{Table}}, throw a {{LinkError}} exception.
2489-
1. Let |tableaddr| be |v|.\[[Table]].
2490-
1. Let |externtable| be the [=external value=] [=external value|table=] |tableaddr|.
2491-
1. [=list/Append=] |externtable| to |imports|.
2461+
1. [=Read an import value=] |v| of type |importtype| into |imports|.
24922462
1. [=Instantiate the core of a WebAssembly module=] |module| with |imports|, and let |coreInstance| be the result.
24932463
1. Let |instanceObject| be a [=/new=] {{Instance}}.
24942464
1. [=initialize an instance object|Initialize=] |instanceObject| from |module| and |coreInstance|.

0 commit comments

Comments
 (0)