jz 0.0.0 → 0.1.1

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,175 @@
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': ['string'],
65
+ 'Object.entries': ['string'],
66
+ 'Date.now': ['core', 'console'],
67
+ 'performance.now': ['core', 'console'],
68
+ 'String.fromCharCode': ['core', 'string'],
69
+ 'String.fromCodePoint': ['core', 'string'],
70
+ 'BigInt.asIntN': ['number'],
71
+ 'BigInt.asUintN': ['number'],
72
+ 'Float64Array.from': ['core', 'typedarray', 'array'],
73
+ 'Float32Array.from': ['core', 'typedarray', 'array'],
74
+ 'Int32Array.from': ['core', 'typedarray', 'array'],
75
+ 'Uint32Array.from': ['core', 'typedarray', 'array'],
76
+ 'Int16Array.from': ['core', 'typedarray', 'array'],
77
+ 'Uint16Array.from': ['core', 'typedarray', 'array'],
78
+ 'Int8Array.from': ['core', 'typedarray', 'array'],
79
+ 'Uint8Array.from': ['core', 'typedarray', 'array'],
80
+ 'ArrayBuffer.isView': ['core', 'typedarray'],
81
+ })
82
+
83
+ export const GENERIC_METHOD_MODULES = dict({
84
+ toString: ['core', 'string', 'number'],
85
+ toFixed: ['core', 'string', 'number'],
86
+ toPrecision: ['core', 'string', 'number'],
87
+ toExponential: ['core', 'string', 'number'],
88
+ })
89
+
90
+ export const CTORS = ['Float64Array','Float32Array','Int32Array','Uint32Array','Int16Array','Uint16Array','Int8Array','Uint8Array','BigInt64Array','BigUint64Array','Set','Map']
91
+ export const TYPED_CTORS = ['Float64Array','Float32Array','Int32Array','Uint32Array','Int16Array','Uint16Array','Int8Array','Uint8Array','BigInt64Array','BigUint64Array','ArrayBuffer','DataView']
92
+ export const COLLECTION_CTORS = ['Set', 'Map']
93
+ export const TIMER_NAMES = new Set(['setTimeout', 'clearTimeout', 'setInterval', 'clearInterval'])
94
+
95
+ export const MOD_ALIAS = { Number: 'number', Array: 'array', Object: 'object', Symbol: 'symbol', JSON: 'json', BigInt: 'number', Error: 'core', TextEncoder: 'string', TextDecoder: 'string' }
96
+
97
+ const MOD_DEPS = {
98
+ number: ['core', 'string'],
99
+ string: ['core', 'number'],
100
+ array: ['core'],
101
+ object: ['core'],
102
+ collection: ['core', 'number'],
103
+ symbol: ['core'],
104
+ json: ['core', 'string', 'number', 'collection'],
105
+ console: ['core', 'string', 'number'],
106
+ regex: ['core', 'string', 'array'],
107
+ }
108
+
109
+ export const hasModule = name => Boolean(mods[MOD_ALIAS[name] || name])
110
+
111
+ export const includeMods = (...names) => names.forEach(includeModule)
112
+
113
+ export const includeForOp = op => {
114
+ const modules = OP_MODULES[op]
115
+ if (!modules) return false
116
+ includeMods(...modules)
117
+ return true
118
+ }
119
+
120
+ export const includeForCallableValue = () => includeMods('core', 'fn')
121
+ export const includeForNumericCoercion = () => includeMods('number', 'string')
122
+ export const includeForStringValue = () => includeMods('core', 'string', 'number')
123
+ export const includeForStringOnly = () => includeMods('core', 'string')
124
+ export const includeForArrayLiteral = () => includeMods('core', 'array')
125
+ export const includeForArrayAccess = () => includeMods('core', 'array', 'collection')
126
+ export const includeForArrayPattern = includeForArrayAccess
127
+ export const includeForObjectLiteral = () => includeMods('core', 'object')
128
+ export const includeForObjectPattern = () => includeMods('core', 'object', 'string', 'collection')
129
+ export const includeForKnownKeyIteration = includeForStringOnly
130
+ export const includeForRuntimeKeyIteration = () => includeMods('core', 'string', 'collection')
131
+ export const includeForTimerRuntime = () => {
132
+ ctx.features.timers = true
133
+ includeModule('timer')
134
+ includeModule('fn')
135
+ }
136
+
137
+ export const includeForNamedCall = callee => {
138
+ const modules = CALL_MODULES[callee]
139
+ if (!modules) return false
140
+ includeMods(...modules)
141
+ return true
142
+ }
143
+
144
+ export const includeForGenericMethod = prop => {
145
+ const modules = GENERIC_METHOD_MODULES[prop]
146
+ if (!modules) return false
147
+ includeMods(...modules)
148
+ return true
149
+ }
150
+
151
+ export const includeForProperty = prop => {
152
+ if (prop === 'byteLength' || prop === 'byteOffset' || prop === 'buffer') includeMods('core', 'typedarray')
153
+ if (typeof prop === 'string' && PROP_MODULES[prop]) includeMods(...PROP_MODULES[prop])
154
+ else includeMods('core', 'object', 'array', 'string', 'collection')
155
+ }
156
+
157
+ export const runtimeCtorKind = name =>
158
+ TYPED_CTORS.includes(name) ? 'typedarray' : COLLECTION_CTORS.includes(name) ? 'collection' : null
159
+
160
+ export const includeForRuntimeCtor = name => {
161
+ const kind = runtimeCtorKind(name)
162
+ if (kind === 'typedarray') includeMods('core', 'typedarray')
163
+ else if (kind === 'collection') includeMods('core', 'collection')
164
+ return kind
165
+ }
166
+
167
+ export function includeModule(name) {
168
+ const modName = MOD_ALIAS[name] || name
169
+ const init = mods[modName]
170
+ if (!init) return err(`Module not found: ${name}`)
171
+ if (ctx.module.modules[modName]) return
172
+ ctx.module.modules[modName] = true
173
+ for (const dep of MOD_DEPS[modName] || []) includeModule(dep)
174
+ init(ctx)
175
+ }