@@ -9,12 +9,50 @@ const { decode } = require("@webassemblyjs/wasm-parser");
99
1010const { Tapable } = require ( "tapable" ) ;
1111const WebAssemblyImportDependency = require ( "./dependencies/WebAssemblyImportDependency" ) ;
12+ const { readFileSync } = require ( "fs" ) ;
13+ const { join } = require ( "path" ) ;
1214
1315const decoderOpts = {
1416 ignoreCodeSection : true ,
1517 ignoreDataSection : true
1618} ;
1719
20+ // FIXME(sven): probably a better way of doing this?
21+ function getSource ( context , request ) {
22+ const path = require . resolve ( join ( context , request ) ) ;
23+ return readFileSync ( path , "utf8" ) ;
24+ }
25+
26+ // FIXME(sven): probably a better way of doing this?
27+ function ensureExported ( source , request ) {
28+ const hasExport = source . indexOf ( "export " ) !== - 1 ;
29+
30+ if ( hasExport === false ) {
31+ const error = new Error (
32+ request + " must export the Memory or Table object."
33+ ) ;
34+
35+ error . stack = "" ;
36+
37+ throw error ;
38+ }
39+ }
40+
41+ // FIXME(sven): probably a better way of doing this?
42+ function ensureIdentNotAccessed ( source , request , ident ) {
43+ const occurence = source . match ( new RegExp ( ident , "g" ) ) . length ;
44+
45+ if ( occurence > 1 ) {
46+ const error = new Error (
47+ request + " is not allowed to access the Memory or Table object (yet)."
48+ ) ;
49+
50+ error . stack = "" ;
51+
52+ throw error ;
53+ }
54+ }
55+
1856class WebAssemblyParser extends Tapable {
1957 constructor ( options ) {
2058 super ( ) ;
@@ -44,6 +82,15 @@ class WebAssemblyParser extends Tapable {
4482 ) ;
4583
4684 state . module . addDependency ( dep ) ;
85+
86+ // To avoid unexpected behaviors we check that the Memory or Table
87+ // has been exported and is not accessed by the user.
88+ if ( node . descr . type === "Memory" || node . descr . type === "Table" ) {
89+ const content = getSource ( state . module . context , node . module ) ;
90+
91+ ensureExported ( content , node . module ) ;
92+ ensureIdentNotAccessed ( content , node . module , node . name ) ;
93+ }
4794 }
4895 } ) ;
4996
0 commit comments