@webqit/oohtml 2.1.34 → 2.1.35
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.gitignore +3 -3
- package/LICENSE +20 -20
- package/README.md +733 -67
- package/dist/bindings-api.js +1 -1
- package/dist/bindings-api.js.map +3 -3
- package/dist/context-api.js +1 -1
- package/dist/context-api.js.map +3 -3
- package/dist/html-imports.js +1 -1
- package/dist/html-imports.js.map +3 -3
- package/dist/main.js +12 -12
- package/dist/main.js.map +3 -3
- package/dist/namespace-api.js +1 -1
- package/dist/namespace-api.js.map +3 -3
- package/dist/scoped-css.js +2 -2
- package/dist/scoped-css.js.map +3 -3
- package/dist/scoped-js.js +7 -7
- package/dist/scoped-js.js.map +3 -3
- package/package.json +76 -76
- package/src/bindings-api/index.js +83 -83
- package/src/bindings-api/targets.browser.js +10 -10
- package/src/context-api/HTMLContext.js +76 -157
- package/src/context-api/HTMLContextProvider.js +158 -0
- package/src/context-api/_ContextRequestEvent.js +25 -25
- package/src/context-api/index.js +51 -51
- package/src/context-api/targets.browser.js +9 -9
- package/src/{html-modules/HTMLExportsManager.js → html-imports/_HTMLExportsManager.js} +185 -199
- package/src/html-imports/_HTMLImportElement.js +211 -213
- package/src/{html-modules/_HTMLImportsContext.js → html-imports/_HTMLImportsProvider.js} +122 -114
- package/src/html-imports/index.js +197 -88
- package/src/html-imports/targets.browser.js +9 -9
- package/src/index.js +30 -32
- package/src/namespace-api/index.js +144 -144
- package/src/namespace-api/targets.browser.js +10 -10
- package/src/scoped-css/index.js +45 -45
- package/src/scoped-css/targets.browser.js +10 -10
- package/src/scoped-js/Compiler.js +297 -297
- package/src/scoped-js/index.js +112 -112
- package/src/scoped-js/targets.browser.js +10 -10
- package/src/targets.browser.js +9 -9
- package/src/util.js +34 -34
- package/test/bindings-api.test.js +42 -42
- package/test/imports.test.js +221 -221
- package/test/index.js +50 -50
- package/test/modules.test.js +200 -200
- package/test/namespace-api.test.js +51 -51
- package/test/scoped-css.test.js +31 -31
- package/test/scoped-js.test.js +29 -29
- package/dist/html-modules.js +0 -2
- package/dist/html-modules.js.map +0 -7
- package/src/context-api/HTMLContextManager.js +0 -77
- package/src/html-modules/index.js +0 -131
- package/src/html-modules/targets.browser.js +0 -10
|
@@ -1,298 +1,298 @@
|
|
|
1
|
-
|
|
2
|
-
export default class Compiler {
|
|
3
|
-
|
|
4
|
-
// Unique ID generator
|
|
5
|
-
static hashTable = new Map;
|
|
6
|
-
static uniqId = () => (0|Math.random()*9e6).toString(36);
|
|
7
|
-
|
|
8
|
-
// Hash of anything generator
|
|
9
|
-
static toHash( val ) {
|
|
10
|
-
let hash;
|
|
11
|
-
if ( !( hash = this.hashTable.get( val ) ) ) {
|
|
12
|
-
hash = this.uniqId();
|
|
13
|
-
this.hashTable.set( val, hash );
|
|
14
|
-
}
|
|
15
|
-
return hash;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
// Value of any hash
|
|
19
|
-
static fromHash( hash ) {
|
|
20
|
-
let val;
|
|
21
|
-
this.hashTable.forEach( ( _hash, _val ) => {
|
|
22
|
-
if ( _hash === hash ) val = _val;
|
|
23
|
-
} );
|
|
24
|
-
return val;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
// Set window property
|
|
28
|
-
constructor( window, config, executeCallback ) {
|
|
29
|
-
this.window = window;
|
|
30
|
-
this.config = config;
|
|
31
|
-
// This is a global function
|
|
32
|
-
window.webqit.oohtml.Script.run = execHash => {
|
|
33
|
-
const exec = this.constructor.fromHash( execHash );
|
|
34
|
-
if ( !exec ) throw new Error( `Argument must be a valid exec hash.` );
|
|
35
|
-
const { script, compiledScript, thisContext } = exec;
|
|
36
|
-
if ( thisContext instanceof window.Element && script.scoped ) {
|
|
37
|
-
if ( !thisContext.scripts ) { Object.defineProperty( thisContext, 'scripts', { value: new Set } ); }
|
|
38
|
-
thisContext.scripts.add( script );
|
|
39
|
-
}
|
|
40
|
-
switch ( config.script.retention ) {
|
|
41
|
-
case 'dispose':
|
|
42
|
-
script.remove();
|
|
43
|
-
break;
|
|
44
|
-
case 'hidden':
|
|
45
|
-
script.textContent = `"source hidden"`;
|
|
46
|
-
break;
|
|
47
|
-
default:
|
|
48
|
-
script.textContent = compiledScript.function.originalSource;
|
|
49
|
-
}
|
|
50
|
-
return executeCallback.call( window, compiledScript, thisContext, script );
|
|
51
|
-
};
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
// Compile scipt
|
|
55
|
-
compile( script, thisContext ) {
|
|
56
|
-
const _static = this.constructor;
|
|
57
|
-
const { webqit: { oohtml, SubscriptFunction } } = this.window;
|
|
58
|
-
const cache = oohtml.Script.compileCache[ script.contract ? 0 : 1 ];
|
|
59
|
-
const sourceHash = _static.toHash( script.textContent );
|
|
60
|
-
// Script instances are parsed only once
|
|
61
|
-
let source = script.textContent, compiledScript;
|
|
62
|
-
if ( !( compiledScript = cache.get( sourceHash ) ) ) {
|
|
63
|
-
// Are there "import" (and "await") statements? Then, we need to rewrite that
|
|
64
|
-
let imports = [], meta = {};
|
|
65
|
-
let targetKeywords = [];
|
|
66
|
-
if ( script.type === 'module' ) targetKeywords.push( 'import ' );
|
|
67
|
-
if ( script.type === 'module' && !script.contract ) targetKeywords.push( 'await ' );
|
|
68
|
-
if ( targetKeywords.length && ( new RegExp( targetKeywords.join( '|' ) ) ).test( source ) ) {
|
|
69
|
-
[ imports, source, meta ] = this.parse( source );
|
|
70
|
-
if ( imports.length ) {
|
|
71
|
-
source = `\n\t${ this.rewriteImportStmts( imports ).join( `\n\t` ) }\n\t${ source }\n`;
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
// Let's obtain a material functions
|
|
75
|
-
let _Function, { parserParams, compilerParams, runtimeParams } = this.config.advanced;
|
|
76
|
-
if ( script.contract ) {
|
|
77
|
-
parserParams = { ...parserParams, allowAwaitOutsideFunction: script.type === 'module' };
|
|
78
|
-
runtimeParams = { ...runtimeParams, async: script.type === 'module' };
|
|
79
|
-
_Function = SubscriptFunction( source, { compilerParams, parserParams, runtimeParams, } );
|
|
80
|
-
Object.defineProperty( script, 'properties', { configurable: true, value: SubscriptFunction.inspect( _Function, 'properties' ) } );
|
|
81
|
-
} else {
|
|
82
|
-
const isAsync = script.type === 'module'//meta.topLevelAwait || imports.length;
|
|
83
|
-
const _FunctionConstructor = isAsync ? Object.getPrototypeOf( async function() {} ).constructor : Function;
|
|
84
|
-
_Function = runtimeParams?.compileFunction
|
|
85
|
-
? runtimeParams.compileFunction( source )
|
|
86
|
-
: new _FunctionConstructor( source );
|
|
87
|
-
}
|
|
88
|
-
Object.defineProperty( _Function, 'originalSource', { configurable: true, value: script.textContent } );
|
|
89
|
-
// Save material function to compile cache
|
|
90
|
-
compiledScript = Object.defineProperty( script.cloneNode(), 'function', { value: _Function } );
|
|
91
|
-
script.scoped && Object.defineProperty( compiledScript, 'scoped', Object.getOwnPropertyDescriptor( script, 'scoped') );
|
|
92
|
-
script.contract && Object.defineProperty( compiledScript, 'contract', Object.getOwnPropertyDescriptor( script, 'contract') );
|
|
93
|
-
cache.set( sourceHash, compiledScript );
|
|
94
|
-
}
|
|
95
|
-
const execHash = _static.toHash( { script, compiledScript, thisContext } );
|
|
96
|
-
if ( script.handling === 'manual' ) {
|
|
97
|
-
webqit.oohtml.Script.run( execHash );
|
|
98
|
-
} else {
|
|
99
|
-
script.textContent = `webqit.oohtml.Script.run( '${ execHash }' );`;
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
// Match import statements
|
|
104
|
-
// and detect top-level await
|
|
105
|
-
parse( source ) {
|
|
106
|
-
const [ tokens, meta ] = this.tokenize( source, ( $tokens, event, char, meta, i, isLastChar ) => {
|
|
107
|
-
|
|
108
|
-
if ( event === 'start-enclosure' && char === '{' && !meta.openAsync?.type && meta.openEnclosures.length === meta.openAsync?.scopeId ) {
|
|
109
|
-
meta.openAsync.type = 'block';
|
|
110
|
-
} else if ( event === 'end-enclosure' && char === '}' && meta.openAsync?.type === 'block' && meta.openEnclosures.length === meta.openAsync.scopeId ) {
|
|
111
|
-
meta.openAsync = null;
|
|
112
|
-
} else if ( event === 'start-quote' && !meta.openEnclosures.length && [ 'starting', 'from' ].includes( meta.openImport ) ) {
|
|
113
|
-
meta.openImport = 'url';
|
|
114
|
-
} else if ( event === 'end-quote' && meta.openImport === 'url' ) {
|
|
115
|
-
meta.openImport = 'closing';
|
|
116
|
-
} else if ( event === 'char' ) {
|
|
117
|
-
|
|
118
|
-
if ( meta.openImport === 'closing' && (
|
|
119
|
-
char === ';'/* explicit */ || ![ ' ', `\n` ].includes( char )/* implicit */ || isLastChar
|
|
120
|
-
) ) {
|
|
121
|
-
if ( char === ';' || isLastChar ) {
|
|
122
|
-
$tokens[ 0 ] += char;
|
|
123
|
-
$tokens.unshift( '' );
|
|
124
|
-
} else { $tokens.unshift( char ); }
|
|
125
|
-
meta.openImport = null;
|
|
126
|
-
return false;
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
let remainder = source.substring( i - 1 );
|
|
130
|
-
|
|
131
|
-
if ( !meta.openImport && /^[\W]?import[ ]*[^\(]/.test( remainder ) ) {
|
|
132
|
-
meta.openImport = 'starting';
|
|
133
|
-
$tokens.unshift( '' );
|
|
134
|
-
return 6;
|
|
135
|
-
}
|
|
136
|
-
if ( meta.openImport === 'starting' && /^[\W]?from /.test( remainder ) ) {
|
|
137
|
-
meta.openImport = 'from';
|
|
138
|
-
return 4;
|
|
139
|
-
}
|
|
140
|
-
if ( !meta.openAsync ) {
|
|
141
|
-
if ( /^[\W]?async /.test( remainder ) ) {
|
|
142
|
-
meta.openAsync = { scopeId: meta.openEnclosures.length };
|
|
143
|
-
return 5;
|
|
144
|
-
}
|
|
145
|
-
if ( /^[\W]?await /.test( remainder ) ) {
|
|
146
|
-
meta.topLevelAwait = true;
|
|
147
|
-
return 5;
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
if ( meta.openAsync ) {
|
|
151
|
-
if ( !meta.openAsync.type && /.?\=\>[ ]*?\{/.test( remainder ) ) {
|
|
152
|
-
meta.openAsync.type = 'inline-arrow';
|
|
153
|
-
} else if ( meta.openAsync.type === 'inline-arrow' && [ `\n`, ';' ].includes( char ) && meta.openEnclosures.length === meta.openAsync.scopeId ) {
|
|
154
|
-
meta.openAsync = null;
|
|
155
|
-
}
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
} );
|
|
161
|
-
// Hoist all import statements
|
|
162
|
-
let imports = [], body = '', _str;
|
|
163
|
-
for ( const str of tokens.reverse() ) {
|
|
164
|
-
if ( ( _str = str.trim() ).startsWith( 'import ' ) ) {
|
|
165
|
-
imports.push( str );
|
|
166
|
-
} else if ( _str ) { body += str; }
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
return [ imports, body, meta ];
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
// Rewrite import statements
|
|
173
|
-
rewriteImportStmts( imports ) {
|
|
174
|
-
const importSpecs = [], importPromises = [];
|
|
175
|
-
imports.forEach( ( $import, i ) => {
|
|
176
|
-
$import = parseImportStmt( $import );
|
|
177
|
-
// Identify whole imports and individual imports
|
|
178
|
-
const [ wholeImport, individualImports ] = $import.items.reduce( ( [ whole, parts ], item ) => {
|
|
179
|
-
return item.id === '*' ? [ item.alias, parts ] : [ whole, parts.concat( item ) ];
|
|
180
|
-
}, [ null, [] ] );
|
|
181
|
-
if ( wholeImport ) {
|
|
182
|
-
// const main = await import("url");
|
|
183
|
-
importSpecs.push( `const ${ wholeImport } = __$imports$__[${ i }];` );
|
|
184
|
-
}
|
|
185
|
-
if ( individualImports.length ) {
|
|
186
|
-
// const { aa: bb, cc } = await import("url");
|
|
187
|
-
const individualImportsSpec = individualImports.map( item => `${ item.id }${ item.id !== item.alias ? `: ${ item.alias }` : '' }` ).join( ', ' );
|
|
188
|
-
importSpecs.push( `const { ${ individualImportsSpec } } = __$imports$__[${ i }];` );
|
|
189
|
-
}
|
|
190
|
-
importPromises.push( `import("${ $import.url }")` );
|
|
191
|
-
} );
|
|
192
|
-
return [
|
|
193
|
-
`\n\tconst __$imports$__ = await Promise.all([\n\t\t${ importPromises.join( `,\n\t\t` ) }\n\t]);`,
|
|
194
|
-
importSpecs.join( `\n\t` ),
|
|
195
|
-
];
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
// Parse import statements
|
|
199
|
-
parseImportStmt( str ) {
|
|
200
|
-
const getUrl = str => {
|
|
201
|
-
let quo = /^[`'"]/.exec( str );
|
|
202
|
-
return quo && str.substring( 1, str.lastIndexOf( quo[ 0 ] ) );
|
|
203
|
-
}
|
|
204
|
-
let $import = { items: [ { id: '' } ] }, _str = str.replace( 'import', '' ).trim();
|
|
205
|
-
if ( !( $import.url = getUrl( _str ) ) ) {
|
|
206
|
-
this.tokenize( _str, ( $tokens, event, char, meta, i, isLastChar ) => {
|
|
207
|
-
if ( [ 'start-quote', 'ongoing-quote', 'end-quote', 'char' ].includes( event ) ) {
|
|
208
|
-
if ( $import.url ) return;
|
|
209
|
-
if ( !meta.openQuote ) {
|
|
210
|
-
let remainder = _str.substring( i );
|
|
211
|
-
if ( char === ',' ) {
|
|
212
|
-
$import.items.unshift( { id: '' } );
|
|
213
|
-
return;
|
|
214
|
-
}
|
|
215
|
-
if ( remainder.startsWith( ' as ' ) ) {
|
|
216
|
-
$import.items[ 0 ].alias = '';
|
|
217
|
-
return 3;
|
|
218
|
-
}
|
|
219
|
-
if ( remainder.startsWith( ' from ' ) ) {
|
|
220
|
-
$import.url = getUrl( remainder.replace( 'from', '' ).trim() );
|
|
221
|
-
return remainder.length;
|
|
222
|
-
}
|
|
223
|
-
}
|
|
224
|
-
if ( 'alias' in $import.items[ 0 ] ) {
|
|
225
|
-
$import.items[ 0 ].alias += char;
|
|
226
|
-
} else {
|
|
227
|
-
$import.items[ 0 ].id += char;
|
|
228
|
-
if ( meta.openEnclosures.length ) {
|
|
229
|
-
$import.items[ 0 ].enclosed = true;
|
|
230
|
-
}
|
|
231
|
-
}
|
|
232
|
-
}
|
|
233
|
-
} );
|
|
234
|
-
}
|
|
235
|
-
$import.items = $import.items
|
|
236
|
-
.map( item => ( {
|
|
237
|
-
id: item.id && !item.alias && !item.enclosed ? 'default' : item.id.trim(),
|
|
238
|
-
alias: item.alias ? item.alias.trim() : item.id.trim(),
|
|
239
|
-
} ) )
|
|
240
|
-
.filter( item => item.id )
|
|
241
|
-
.reverse();
|
|
242
|
-
return $import;
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
// Token JavaScript source
|
|
246
|
-
tokenize( source, _callback ) {
|
|
247
|
-
const lastI = source.length - 1;
|
|
248
|
-
return [ ...source ].reduce( ( [ $tokens, meta, skip ], char, i ) => {
|
|
249
|
-
|
|
250
|
-
if ( skip ) {
|
|
251
|
-
$tokens[ 0 ] += char;
|
|
252
|
-
return [ $tokens, meta, --skip ];
|
|
253
|
-
}
|
|
254
|
-
let callbackReturn;
|
|
255
|
-
|
|
256
|
-
if ( meta.openQuote || meta.openComment ) {
|
|
257
|
-
if ( char === meta.openQuote ) {
|
|
258
|
-
meta.openQuote = null;
|
|
259
|
-
callbackReturn = _callback( $tokens, 'end-quote', char, meta, i, i === lastI );
|
|
260
|
-
} else if ( meta.openQuote ) {
|
|
261
|
-
callbackReturn = _callback( $tokens, 'ongoing-quote', char, meta, i, i === lastI );
|
|
262
|
-
} else if ( meta.openComment ) {
|
|
263
|
-
if ( ( meta.openComment === '//' && char === `\n` ) || ( meta.openComment === '/*' && $tokens[ 0 ].substr( -1 ) === '*' && char === '/' ) ) {
|
|
264
|
-
meta.openComment = null;
|
|
265
|
-
callbackReturn = _callback( $tokens, 'end-comment', char, meta, i, i === lastI );
|
|
266
|
-
}
|
|
267
|
-
}
|
|
268
|
-
if ( callbackReturn !== false ) {
|
|
269
|
-
$tokens[ 0 ] += char;
|
|
270
|
-
}
|
|
271
|
-
return [ $tokens, meta, typeof callbackReturn === 'number' ? callbackReturn : skip ];
|
|
272
|
-
}
|
|
273
|
-
|
|
274
|
-
let enclosure;
|
|
275
|
-
if ( enclosure = [ '()', '{}', '[]' ].filter( pair => char === pair[ 0 ] )[ 0 ] ) {
|
|
276
|
-
callbackReturn = _callback( $tokens, 'start-enclosure', char, meta, i, i === lastI );
|
|
277
|
-
meta.openEnclosures.unshift( enclosure );
|
|
278
|
-
} else if ( meta.openEnclosures.length && char === meta.openEnclosures[ 0 ][ 1 ] ) {
|
|
279
|
-
meta.openEnclosures.shift();
|
|
280
|
-
callbackReturn = _callback( $tokens, 'end-enclosure', char, meta, i, i === lastI );
|
|
281
|
-
} else if ( [ '"', "'", "`" ].includes( char ) ) {
|
|
282
|
-
callbackReturn = _callback( $tokens, 'start-quote', char, meta, i, i === lastI );
|
|
283
|
-
meta.openQuote = char;
|
|
284
|
-
} else if ( !meta.openComment && [ '/*', '//' ].includes( source.substr( i, 2 ) ) ) {
|
|
285
|
-
callbackReturn = _callback( $tokens, 'start-comment', char, meta, i, i === lastI );
|
|
286
|
-
meta.openComment = source.substr( i, 2 );
|
|
287
|
-
} else {
|
|
288
|
-
callbackReturn = _callback( $tokens, 'char', char, meta, i, i === lastI );
|
|
289
|
-
}
|
|
290
|
-
|
|
291
|
-
if ( callbackReturn !== false ) {
|
|
292
|
-
$tokens[ 0 ] += char;
|
|
293
|
-
}
|
|
294
|
-
return [ $tokens, meta, typeof callbackReturn === 'number' ? callbackReturn : skip ];
|
|
295
|
-
|
|
296
|
-
}, [ [ '' ], { openEnclosures: [], }, 0 ] );
|
|
297
|
-
}
|
|
1
|
+
|
|
2
|
+
export default class Compiler {
|
|
3
|
+
|
|
4
|
+
// Unique ID generator
|
|
5
|
+
static hashTable = new Map;
|
|
6
|
+
static uniqId = () => (0|Math.random()*9e6).toString(36);
|
|
7
|
+
|
|
8
|
+
// Hash of anything generator
|
|
9
|
+
static toHash( val ) {
|
|
10
|
+
let hash;
|
|
11
|
+
if ( !( hash = this.hashTable.get( val ) ) ) {
|
|
12
|
+
hash = this.uniqId();
|
|
13
|
+
this.hashTable.set( val, hash );
|
|
14
|
+
}
|
|
15
|
+
return hash;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
// Value of any hash
|
|
19
|
+
static fromHash( hash ) {
|
|
20
|
+
let val;
|
|
21
|
+
this.hashTable.forEach( ( _hash, _val ) => {
|
|
22
|
+
if ( _hash === hash ) val = _val;
|
|
23
|
+
} );
|
|
24
|
+
return val;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// Set window property
|
|
28
|
+
constructor( window, config, executeCallback ) {
|
|
29
|
+
this.window = window;
|
|
30
|
+
this.config = config;
|
|
31
|
+
// This is a global function
|
|
32
|
+
window.webqit.oohtml.Script.run = execHash => {
|
|
33
|
+
const exec = this.constructor.fromHash( execHash );
|
|
34
|
+
if ( !exec ) throw new Error( `Argument must be a valid exec hash.` );
|
|
35
|
+
const { script, compiledScript, thisContext } = exec;
|
|
36
|
+
if ( thisContext instanceof window.Element && script.scoped ) {
|
|
37
|
+
if ( !thisContext.scripts ) { Object.defineProperty( thisContext, 'scripts', { value: new Set } ); }
|
|
38
|
+
thisContext.scripts.add( script );
|
|
39
|
+
}
|
|
40
|
+
switch ( config.script.retention ) {
|
|
41
|
+
case 'dispose':
|
|
42
|
+
script.remove();
|
|
43
|
+
break;
|
|
44
|
+
case 'hidden':
|
|
45
|
+
script.textContent = `"source hidden"`;
|
|
46
|
+
break;
|
|
47
|
+
default:
|
|
48
|
+
script.textContent = compiledScript.function.originalSource;
|
|
49
|
+
}
|
|
50
|
+
return executeCallback.call( window, compiledScript, thisContext, script );
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// Compile scipt
|
|
55
|
+
compile( script, thisContext ) {
|
|
56
|
+
const _static = this.constructor;
|
|
57
|
+
const { webqit: { oohtml, SubscriptFunction } } = this.window;
|
|
58
|
+
const cache = oohtml.Script.compileCache[ script.contract ? 0 : 1 ];
|
|
59
|
+
const sourceHash = _static.toHash( script.textContent );
|
|
60
|
+
// Script instances are parsed only once
|
|
61
|
+
let source = script.textContent, compiledScript;
|
|
62
|
+
if ( !( compiledScript = cache.get( sourceHash ) ) ) {
|
|
63
|
+
// Are there "import" (and "await") statements? Then, we need to rewrite that
|
|
64
|
+
let imports = [], meta = {};
|
|
65
|
+
let targetKeywords = [];
|
|
66
|
+
if ( script.type === 'module' ) targetKeywords.push( 'import ' );
|
|
67
|
+
if ( script.type === 'module' && !script.contract ) targetKeywords.push( 'await ' );
|
|
68
|
+
if ( targetKeywords.length && ( new RegExp( targetKeywords.join( '|' ) ) ).test( source ) ) {
|
|
69
|
+
[ imports, source, meta ] = this.parse( source );
|
|
70
|
+
if ( imports.length ) {
|
|
71
|
+
source = `\n\t${ this.rewriteImportStmts( imports ).join( `\n\t` ) }\n\t${ source }\n`;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
// Let's obtain a material functions
|
|
75
|
+
let _Function, { parserParams, compilerParams, runtimeParams } = this.config.advanced;
|
|
76
|
+
if ( script.contract ) {
|
|
77
|
+
parserParams = { ...parserParams, allowAwaitOutsideFunction: script.type === 'module' };
|
|
78
|
+
runtimeParams = { ...runtimeParams, async: script.type === 'module' };
|
|
79
|
+
_Function = SubscriptFunction( source, { compilerParams, parserParams, runtimeParams, } );
|
|
80
|
+
Object.defineProperty( script, 'properties', { configurable: true, value: SubscriptFunction.inspect( _Function, 'properties' ) } );
|
|
81
|
+
} else {
|
|
82
|
+
const isAsync = script.type === 'module'//meta.topLevelAwait || imports.length;
|
|
83
|
+
const _FunctionConstructor = isAsync ? Object.getPrototypeOf( async function() {} ).constructor : Function;
|
|
84
|
+
_Function = runtimeParams?.compileFunction
|
|
85
|
+
? runtimeParams.compileFunction( source )
|
|
86
|
+
: new _FunctionConstructor( source );
|
|
87
|
+
}
|
|
88
|
+
Object.defineProperty( _Function, 'originalSource', { configurable: true, value: script.textContent } );
|
|
89
|
+
// Save material function to compile cache
|
|
90
|
+
compiledScript = Object.defineProperty( script.cloneNode(), 'function', { value: _Function } );
|
|
91
|
+
script.scoped && Object.defineProperty( compiledScript, 'scoped', Object.getOwnPropertyDescriptor( script, 'scoped') );
|
|
92
|
+
script.contract && Object.defineProperty( compiledScript, 'contract', Object.getOwnPropertyDescriptor( script, 'contract') );
|
|
93
|
+
cache.set( sourceHash, compiledScript );
|
|
94
|
+
}
|
|
95
|
+
const execHash = _static.toHash( { script, compiledScript, thisContext } );
|
|
96
|
+
if ( script.handling === 'manual' ) {
|
|
97
|
+
webqit.oohtml.Script.run( execHash );
|
|
98
|
+
} else {
|
|
99
|
+
script.textContent = `webqit.oohtml.Script.run( '${ execHash }' );`;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// Match import statements
|
|
104
|
+
// and detect top-level await
|
|
105
|
+
parse( source ) {
|
|
106
|
+
const [ tokens, meta ] = this.tokenize( source, ( $tokens, event, char, meta, i, isLastChar ) => {
|
|
107
|
+
|
|
108
|
+
if ( event === 'start-enclosure' && char === '{' && !meta.openAsync?.type && meta.openEnclosures.length === meta.openAsync?.scopeId ) {
|
|
109
|
+
meta.openAsync.type = 'block';
|
|
110
|
+
} else if ( event === 'end-enclosure' && char === '}' && meta.openAsync?.type === 'block' && meta.openEnclosures.length === meta.openAsync.scopeId ) {
|
|
111
|
+
meta.openAsync = null;
|
|
112
|
+
} else if ( event === 'start-quote' && !meta.openEnclosures.length && [ 'starting', 'from' ].includes( meta.openImport ) ) {
|
|
113
|
+
meta.openImport = 'url';
|
|
114
|
+
} else if ( event === 'end-quote' && meta.openImport === 'url' ) {
|
|
115
|
+
meta.openImport = 'closing';
|
|
116
|
+
} else if ( event === 'char' ) {
|
|
117
|
+
|
|
118
|
+
if ( meta.openImport === 'closing' && (
|
|
119
|
+
char === ';'/* explicit */ || ![ ' ', `\n` ].includes( char )/* implicit */ || isLastChar
|
|
120
|
+
) ) {
|
|
121
|
+
if ( char === ';' || isLastChar ) {
|
|
122
|
+
$tokens[ 0 ] += char;
|
|
123
|
+
$tokens.unshift( '' );
|
|
124
|
+
} else { $tokens.unshift( char ); }
|
|
125
|
+
meta.openImport = null;
|
|
126
|
+
return false;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
let remainder = source.substring( i - 1 );
|
|
130
|
+
|
|
131
|
+
if ( !meta.openImport && /^[\W]?import[ ]*[^\(]/.test( remainder ) ) {
|
|
132
|
+
meta.openImport = 'starting';
|
|
133
|
+
$tokens.unshift( '' );
|
|
134
|
+
return 6;
|
|
135
|
+
}
|
|
136
|
+
if ( meta.openImport === 'starting' && /^[\W]?from /.test( remainder ) ) {
|
|
137
|
+
meta.openImport = 'from';
|
|
138
|
+
return 4;
|
|
139
|
+
}
|
|
140
|
+
if ( !meta.openAsync ) {
|
|
141
|
+
if ( /^[\W]?async /.test( remainder ) ) {
|
|
142
|
+
meta.openAsync = { scopeId: meta.openEnclosures.length };
|
|
143
|
+
return 5;
|
|
144
|
+
}
|
|
145
|
+
if ( /^[\W]?await /.test( remainder ) ) {
|
|
146
|
+
meta.topLevelAwait = true;
|
|
147
|
+
return 5;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
if ( meta.openAsync ) {
|
|
151
|
+
if ( !meta.openAsync.type && /.?\=\>[ ]*?\{/.test( remainder ) ) {
|
|
152
|
+
meta.openAsync.type = 'inline-arrow';
|
|
153
|
+
} else if ( meta.openAsync.type === 'inline-arrow' && [ `\n`, ';' ].includes( char ) && meta.openEnclosures.length === meta.openAsync.scopeId ) {
|
|
154
|
+
meta.openAsync = null;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
} );
|
|
161
|
+
// Hoist all import statements
|
|
162
|
+
let imports = [], body = '', _str;
|
|
163
|
+
for ( const str of tokens.reverse() ) {
|
|
164
|
+
if ( ( _str = str.trim() ).startsWith( 'import ' ) ) {
|
|
165
|
+
imports.push( str );
|
|
166
|
+
} else if ( _str ) { body += str; }
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
return [ imports, body, meta ];
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
// Rewrite import statements
|
|
173
|
+
rewriteImportStmts( imports ) {
|
|
174
|
+
const importSpecs = [], importPromises = [];
|
|
175
|
+
imports.forEach( ( $import, i ) => {
|
|
176
|
+
$import = parseImportStmt( $import );
|
|
177
|
+
// Identify whole imports and individual imports
|
|
178
|
+
const [ wholeImport, individualImports ] = $import.items.reduce( ( [ whole, parts ], item ) => {
|
|
179
|
+
return item.id === '*' ? [ item.alias, parts ] : [ whole, parts.concat( item ) ];
|
|
180
|
+
}, [ null, [] ] );
|
|
181
|
+
if ( wholeImport ) {
|
|
182
|
+
// const main = await import("url");
|
|
183
|
+
importSpecs.push( `const ${ wholeImport } = __$imports$__[${ i }];` );
|
|
184
|
+
}
|
|
185
|
+
if ( individualImports.length ) {
|
|
186
|
+
// const { aa: bb, cc } = await import("url");
|
|
187
|
+
const individualImportsSpec = individualImports.map( item => `${ item.id }${ item.id !== item.alias ? `: ${ item.alias }` : '' }` ).join( ', ' );
|
|
188
|
+
importSpecs.push( `const { ${ individualImportsSpec } } = __$imports$__[${ i }];` );
|
|
189
|
+
}
|
|
190
|
+
importPromises.push( `import("${ $import.url }")` );
|
|
191
|
+
} );
|
|
192
|
+
return [
|
|
193
|
+
`\n\tconst __$imports$__ = await Promise.all([\n\t\t${ importPromises.join( `,\n\t\t` ) }\n\t]);`,
|
|
194
|
+
importSpecs.join( `\n\t` ),
|
|
195
|
+
];
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
// Parse import statements
|
|
199
|
+
parseImportStmt( str ) {
|
|
200
|
+
const getUrl = str => {
|
|
201
|
+
let quo = /^[`'"]/.exec( str );
|
|
202
|
+
return quo && str.substring( 1, str.lastIndexOf( quo[ 0 ] ) );
|
|
203
|
+
}
|
|
204
|
+
let $import = { items: [ { id: '' } ] }, _str = str.replace( 'import', '' ).trim();
|
|
205
|
+
if ( !( $import.url = getUrl( _str ) ) ) {
|
|
206
|
+
this.tokenize( _str, ( $tokens, event, char, meta, i, isLastChar ) => {
|
|
207
|
+
if ( [ 'start-quote', 'ongoing-quote', 'end-quote', 'char' ].includes( event ) ) {
|
|
208
|
+
if ( $import.url ) return;
|
|
209
|
+
if ( !meta.openQuote ) {
|
|
210
|
+
let remainder = _str.substring( i );
|
|
211
|
+
if ( char === ',' ) {
|
|
212
|
+
$import.items.unshift( { id: '' } );
|
|
213
|
+
return;
|
|
214
|
+
}
|
|
215
|
+
if ( remainder.startsWith( ' as ' ) ) {
|
|
216
|
+
$import.items[ 0 ].alias = '';
|
|
217
|
+
return 3;
|
|
218
|
+
}
|
|
219
|
+
if ( remainder.startsWith( ' from ' ) ) {
|
|
220
|
+
$import.url = getUrl( remainder.replace( 'from', '' ).trim() );
|
|
221
|
+
return remainder.length;
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
if ( 'alias' in $import.items[ 0 ] ) {
|
|
225
|
+
$import.items[ 0 ].alias += char;
|
|
226
|
+
} else {
|
|
227
|
+
$import.items[ 0 ].id += char;
|
|
228
|
+
if ( meta.openEnclosures.length ) {
|
|
229
|
+
$import.items[ 0 ].enclosed = true;
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
} );
|
|
234
|
+
}
|
|
235
|
+
$import.items = $import.items
|
|
236
|
+
.map( item => ( {
|
|
237
|
+
id: item.id && !item.alias && !item.enclosed ? 'default' : item.id.trim(),
|
|
238
|
+
alias: item.alias ? item.alias.trim() : item.id.trim(),
|
|
239
|
+
} ) )
|
|
240
|
+
.filter( item => item.id )
|
|
241
|
+
.reverse();
|
|
242
|
+
return $import;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
// Token JavaScript source
|
|
246
|
+
tokenize( source, _callback ) {
|
|
247
|
+
const lastI = source.length - 1;
|
|
248
|
+
return [ ...source ].reduce( ( [ $tokens, meta, skip ], char, i ) => {
|
|
249
|
+
|
|
250
|
+
if ( skip ) {
|
|
251
|
+
$tokens[ 0 ] += char;
|
|
252
|
+
return [ $tokens, meta, --skip ];
|
|
253
|
+
}
|
|
254
|
+
let callbackReturn;
|
|
255
|
+
|
|
256
|
+
if ( meta.openQuote || meta.openComment ) {
|
|
257
|
+
if ( char === meta.openQuote ) {
|
|
258
|
+
meta.openQuote = null;
|
|
259
|
+
callbackReturn = _callback( $tokens, 'end-quote', char, meta, i, i === lastI );
|
|
260
|
+
} else if ( meta.openQuote ) {
|
|
261
|
+
callbackReturn = _callback( $tokens, 'ongoing-quote', char, meta, i, i === lastI );
|
|
262
|
+
} else if ( meta.openComment ) {
|
|
263
|
+
if ( ( meta.openComment === '//' && char === `\n` ) || ( meta.openComment === '/*' && $tokens[ 0 ].substr( -1 ) === '*' && char === '/' ) ) {
|
|
264
|
+
meta.openComment = null;
|
|
265
|
+
callbackReturn = _callback( $tokens, 'end-comment', char, meta, i, i === lastI );
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
if ( callbackReturn !== false ) {
|
|
269
|
+
$tokens[ 0 ] += char;
|
|
270
|
+
}
|
|
271
|
+
return [ $tokens, meta, typeof callbackReturn === 'number' ? callbackReturn : skip ];
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
let enclosure;
|
|
275
|
+
if ( enclosure = [ '()', '{}', '[]' ].filter( pair => char === pair[ 0 ] )[ 0 ] ) {
|
|
276
|
+
callbackReturn = _callback( $tokens, 'start-enclosure', char, meta, i, i === lastI );
|
|
277
|
+
meta.openEnclosures.unshift( enclosure );
|
|
278
|
+
} else if ( meta.openEnclosures.length && char === meta.openEnclosures[ 0 ][ 1 ] ) {
|
|
279
|
+
meta.openEnclosures.shift();
|
|
280
|
+
callbackReturn = _callback( $tokens, 'end-enclosure', char, meta, i, i === lastI );
|
|
281
|
+
} else if ( [ '"', "'", "`" ].includes( char ) ) {
|
|
282
|
+
callbackReturn = _callback( $tokens, 'start-quote', char, meta, i, i === lastI );
|
|
283
|
+
meta.openQuote = char;
|
|
284
|
+
} else if ( !meta.openComment && [ '/*', '//' ].includes( source.substr( i, 2 ) ) ) {
|
|
285
|
+
callbackReturn = _callback( $tokens, 'start-comment', char, meta, i, i === lastI );
|
|
286
|
+
meta.openComment = source.substr( i, 2 );
|
|
287
|
+
} else {
|
|
288
|
+
callbackReturn = _callback( $tokens, 'char', char, meta, i, i === lastI );
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
if ( callbackReturn !== false ) {
|
|
292
|
+
$tokens[ 0 ] += char;
|
|
293
|
+
}
|
|
294
|
+
return [ $tokens, meta, typeof callbackReturn === 'number' ? callbackReturn : skip ];
|
|
295
|
+
|
|
296
|
+
}, [ [ '' ], { openEnclosures: [], }, 0 ] );
|
|
297
|
+
}
|
|
298
298
|
}
|