jz 0.1.0 → 0.2.0

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.
@@ -0,0 +1,183 @@
1
+ /** Runtime module autoload rules used by prepare(). */
2
+
3
+ import { ctx, err } from './ctx.js'
4
+ import * as mods from '../module/index.js'
5
+
6
+ const dict = obj => Object.assign(Object.create(null), obj)
7
+
8
+ export const PROP_MODULES = Object.assign(Object.create(null), {
9
+ push: ['core', 'array'], pop: ['core', 'array'], shift: ['core', 'array'], unshift: ['core', 'array'],
10
+ splice: ['core', 'array'], reverse: ['core', 'array'], sort: ['core', 'array'], fill: ['core', 'array'],
11
+ map: ['core', 'array'], filter: ['core', 'array'], reduce: ['core', 'array'], reduceRight: ['core', 'array'],
12
+ forEach: ['core', 'array'], find: ['core', 'array'], findIndex: ['core', 'array'],
13
+ findLast: ['core', 'array'], findLastIndex: ['core', 'array'],
14
+ every: ['core', 'array'], some: ['core', 'array'], flat: ['core', 'array'], flatMap: ['core', 'array'],
15
+ join: ['core', 'array'], copyWithin: ['core', 'array'], at: ['core', 'array'],
16
+ charAt: ['core', 'string'], charCodeAt: ['core', 'string'], codePointAt: ['core', 'string'],
17
+ toUpperCase: ['core', 'string'], toLowerCase: ['core', 'string'], trim: ['core', 'string'],
18
+ trimStart: ['core', 'string'], trimEnd: ['core', 'string'],
19
+ split: ['core', 'string'], replace: ['core', 'string'], replaceAll: ['core', 'string'],
20
+ repeat: ['core', 'string'], startsWith: ['core', 'string'], endsWith: ['core', 'string'],
21
+ padStart: ['core', 'string'], padEnd: ['core', 'string'], normalize: ['core', 'string'],
22
+ matchAll: ['core', 'string'], match: ['core', 'string'],
23
+ substring: ['core', 'string'], substr: ['core', 'string'],
24
+ add: ['core', 'collection'], clear: ['core', 'collection'],
25
+ slice: ['core', 'string', 'array'], concat: ['core', 'string', 'array'],
26
+ indexOf: ['core', 'string', 'array'], lastIndexOf: ['core', 'string', 'array'],
27
+ includes: ['core', 'string', 'array'],
28
+ length: ['core', 'string', 'array', 'typedarray'],
29
+ })
30
+
31
+ export const OP_MODULES = {
32
+ '?.': ['core', 'string', 'collection'],
33
+ '?.[]': ['core', 'array', 'collection'],
34
+ '?.()': ['core', 'fn'],
35
+ 'u+': ['number', 'string'],
36
+ 'in': ['core', 'collection', 'string'],
37
+ '==': ['core', 'string'],
38
+ '!=': ['core', 'string'],
39
+ 'typeof': ['core', 'string'],
40
+ '[': ['core', 'array'],
41
+ '{': ['core', 'object', 'string', 'collection'],
42
+ '//': ['core', 'string', 'regex'],
43
+ '**': ['math'],
44
+ }
45
+
46
+ export const CALL_MODULES = dict({
47
+ ArrayBuffer: ['core', 'typedarray'],
48
+ DataView: ['core', 'typedarray'],
49
+ BigInt64Array: ['core', 'typedarray'],
50
+ BigUint64Array: ['core', 'typedarray'],
51
+ parseFloat: ['number', 'string'],
52
+ parseInt: ['number', 'string'],
53
+ String: ['core', 'string', 'number'],
54
+ Number: ['number', 'string'],
55
+ Boolean: ['number'],
56
+ TextEncoder: ['core', 'string'],
57
+ TextDecoder: ['core', 'string'],
58
+ Error: ['core', 'string'],
59
+ BigInt: ['number'],
60
+ 'console.log': ['core', 'string', 'number', 'console'],
61
+ 'console.warn': ['core', 'string', 'number', 'console'],
62
+ 'console.error': ['core', 'string', 'number', 'console'],
63
+ 'Object.fromEntries': ['collection', 'string'],
64
+ 'Object.keys': ['core', 'object', 'string'],
65
+ 'Object.values': ['core', 'object', 'string'],
66
+ 'Object.entries': ['core', 'object', 'string'],
67
+ 'Object.assign': ['core', 'object'],
68
+ 'Object.create': ['core', 'object'],
69
+ 'Date.UTC': ['core', 'date'],
70
+ 'Date.now': ['core', 'console'],
71
+ 'performance.now': ['core', 'console'],
72
+ 'readStdin': ['core', 'console'],
73
+ 'String.fromCharCode': ['core', 'string'],
74
+ 'String.fromCodePoint': ['core', 'string'],
75
+ 'BigInt.asIntN': ['number'],
76
+ 'BigInt.asUintN': ['number'],
77
+ 'Float64Array.from': ['core', 'typedarray', 'array'],
78
+ 'Float32Array.from': ['core', 'typedarray', 'array'],
79
+ 'Int32Array.from': ['core', 'typedarray', 'array'],
80
+ 'Uint32Array.from': ['core', 'typedarray', 'array'],
81
+ 'Int16Array.from': ['core', 'typedarray', 'array'],
82
+ 'Uint16Array.from': ['core', 'typedarray', 'array'],
83
+ 'Int8Array.from': ['core', 'typedarray', 'array'],
84
+ 'Uint8Array.from': ['core', 'typedarray', 'array'],
85
+ 'ArrayBuffer.isView': ['core', 'typedarray'],
86
+ })
87
+
88
+ export const GENERIC_METHOD_MODULES = dict({
89
+ toString: ['core', 'string', 'number'],
90
+ toFixed: ['core', 'string', 'number'],
91
+ toPrecision: ['core', 'string', 'number'],
92
+ toExponential: ['core', 'string', 'number'],
93
+ hasOwnProperty: ['core', 'object', 'string', 'collection'],
94
+ })
95
+
96
+ export const CTORS = ['Float64Array','Float32Array','Int32Array','Uint32Array','Int16Array','Uint16Array','Int8Array','Uint8Array','BigInt64Array','BigUint64Array','Set','Map','Date']
97
+ export const TYPED_CTORS = ['Float64Array','Float32Array','Int32Array','Uint32Array','Int16Array','Uint16Array','Int8Array','Uint8Array','BigInt64Array','BigUint64Array','ArrayBuffer','DataView']
98
+ export const COLLECTION_CTORS = ['Set', 'Map']
99
+ export const TIMER_NAMES = new Set(['setTimeout', 'clearTimeout', 'setInterval', 'clearInterval'])
100
+
101
+ export const MOD_ALIAS = { Number: 'number', Array: 'array', Object: 'object', Symbol: 'symbol', JSON: 'json', Date: 'date', BigInt: 'number', Error: 'core', TextEncoder: 'string', TextDecoder: 'string' }
102
+
103
+ const MOD_DEPS = {
104
+ number: ['core', 'string'],
105
+ string: ['core', 'number'],
106
+ array: ['core'],
107
+ object: ['core'],
108
+ collection: ['core', 'number'],
109
+ symbol: ['core'],
110
+ json: ['core', 'string', 'number', 'collection'],
111
+ date: ['core', 'number', 'string'],
112
+ console: ['core', 'string', 'number'],
113
+ regex: ['core', 'string', 'array'],
114
+ }
115
+
116
+ export const hasModule = name => Boolean(mods[MOD_ALIAS[name] || name])
117
+
118
+ export const includeMods = (...names) => names.forEach(includeModule)
119
+
120
+ export const includeForOp = op => {
121
+ const modules = OP_MODULES[op]
122
+ if (!modules) return false
123
+ includeMods(...modules)
124
+ return true
125
+ }
126
+
127
+ export const includeForCallableValue = () => includeMods('core', 'fn')
128
+ export const includeForNumericCoercion = () => includeMods('number', 'string')
129
+ export const includeForStringValue = () => includeMods('core', 'string', 'number')
130
+ export const includeForStringOnly = () => includeMods('core', 'string')
131
+ export const includeForArrayLiteral = () => includeMods('core', 'array')
132
+ export const includeForArrayAccess = () => includeMods('core', 'array', 'collection')
133
+ export const includeForArrayPattern = includeForArrayAccess
134
+ export const includeForObjectLiteral = () => includeMods('core', 'object')
135
+ export const includeForObjectPattern = () => includeMods('core', 'object', 'string', 'collection')
136
+ export const includeForKnownKeyIteration = includeForStringOnly
137
+ export const includeForRuntimeKeyIteration = () => includeMods('core', 'string', 'collection')
138
+ export const includeForTimerRuntime = () => {
139
+ ctx.features.timers = true
140
+ includeModule('timer')
141
+ includeModule('fn')
142
+ }
143
+
144
+ export const includeForNamedCall = callee => {
145
+ const modules = CALL_MODULES[callee]
146
+ if (!modules) return false
147
+ includeMods(...modules)
148
+ return true
149
+ }
150
+
151
+ export const includeForGenericMethod = prop => {
152
+ const modules = GENERIC_METHOD_MODULES[prop]
153
+ if (!modules) return false
154
+ includeMods(...modules)
155
+ return true
156
+ }
157
+
158
+ export const includeForProperty = prop => {
159
+ if (prop === 'byteLength' || prop === 'byteOffset' || prop === 'buffer') includeMods('core', 'typedarray')
160
+ if (typeof prop === 'string' && PROP_MODULES[prop]) includeMods(...PROP_MODULES[prop])
161
+ else includeMods('core', 'object', 'array', 'string', 'collection')
162
+ }
163
+
164
+ export const runtimeCtorKind = name =>
165
+ TYPED_CTORS.includes(name) ? 'typedarray' : COLLECTION_CTORS.includes(name) ? 'collection' : name === 'Date' ? 'date' : null
166
+
167
+ export const includeForRuntimeCtor = name => {
168
+ const kind = runtimeCtorKind(name)
169
+ if (kind === 'typedarray') includeMods('core', 'typedarray')
170
+ else if (kind === 'collection') includeMods('core', 'collection')
171
+ else if (kind === 'date') includeMods('core', 'date')
172
+ return kind
173
+ }
174
+
175
+ export function includeModule(name) {
176
+ const modName = MOD_ALIAS[name] || name
177
+ const init = mods[modName]
178
+ if (!init) return err(`Module not found: ${name}`)
179
+ if (ctx.module.modules[modName]) return
180
+ ctx.module.modules[modName] = true
181
+ for (const dep of MOD_DEPS[modName] || []) includeModule(dep)
182
+ init(ctx)
183
+ }