@tinyactors/wasm 0.3.0 → 0.4.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.
@@ -1,2 +1,2 @@
1
1
  // GENERATED by mise/wasm.rb from src/datamodel/realm.js: the realm's script as text.
2
- export default "// The realm's script of the native \"ecmascript\" datamodel (AGENTS.md, \"The\n// native \"ecmascript\" datamodel\"; src/datamodel/realms.c): run once in every\n// node:vm context the datamodel makes, the host's shared one and each\n// declaring session's own, with In() as its argument. It returns the\n// context's kit, the functions that make what must be made with that\n// context's constructors: copies of values crossing between actors, the\n// scopes of a chart (one constructor per chart, over a frozen prototype of\n// null prototype holding the system variables), the system variables of a\n// session, _event, and XML documents. The build embeds it in the addon\n// (mise/napi.rb); it is not shipped as a file and never runs in the\n// application's realm.\n(function tinyactorsRealm(In, Address) {\n 'use strict';\n const LINK = Symbol('tinyactors.session');\n const XML = Symbol.for('tinyactors.xml');\n const SOURCE = Symbol.for('tinyactors.xml.source');\n const { create, defineProperty, freeze, getPrototypeOf, hasOwn, keys } = Object;\n const tag = Object.prototype.toString;\n const source = Function.prototype.toString;\n const isArray = Array.isArray;\n const isView = ArrayBuffer.isView;\n const names = Object.getOwnPropertyNames;\n // The global's own names before any chart's: what a context starts with,\n // and the system variables a declaring session's context gets (declare).\n const baseline = new Set(names(globalThis));\n const systemNames = new Set(['_sessionid', '_name', '_ioprocessors', 'In', '_event']);\n const indirect = eval; // global code of this context, whatever a chart puts on its global\n const identifier = /^[A-Za-z_$][A-Za-z0-9_$]*$/;\n\n // The errors copy throws for a value that cannot cross, by status: an\n // input refused with them throws a TinyactorsError of that status.\n const refusals = new WeakMap();\n function refuse(status, error) {\n refusals.set(error, status);\n return error;\n }\n\n function own(object, key, value) {\n defineProperty(object, key, { value, writable: true, enumerable: true, configurable: true });\n }\n\n function plain(prototype) {\n if (prototype === null) return true;\n if (getPrototypeOf(prototype) !== null || !hasOwn(prototype, 'constructor')) return false;\n const made = prototype.constructor;\n return typeof made === 'function' && made.prototype === prototype && made.name === 'Object' &&\n source.call(made).includes('[native code]');\n }\n\n class XMLNode {\n constructor(nodeType, nodeName) {\n this.nodeType = nodeType;\n this.nodeName = nodeName;\n this.childNodes = [];\n defineProperty(this, 'parentNode', { value: null, writable: true, configurable: true });\n }\n get firstChild() { return this.childNodes.length ? this.childNodes[0] : null; }\n get lastChild() { return this.childNodes.length ? this.childNodes[this.childNodes.length - 1] : null; }\n get documentElement() {\n if (this.nodeType !== 9) return undefined;\n for (const child of this.childNodes) if (child.nodeType === 1) return child;\n return null;\n }\n get textContent() {\n if (this.nodeType === 3 || this.nodeType === 4) return this.nodeValue;\n let text = '';\n for (const child of this.childNodes) if (child.nodeType !== 8 && child.nodeType !== 7) text += child.textContent;\n return text;\n }\n appendChild(child) {\n child.parentNode = this;\n this.childNodes.push(child);\n return child;\n }\n getElementsByTagName(name) {\n const found = [];\n const pending = this.childNodes.slice().reverse();\n while (pending.length) {\n const node = pending.pop();\n if (node.nodeType !== 1) continue;\n if (name === '*' || node.nodeName === name || node.localName === name) found.push(node);\n for (let i = node.childNodes.length - 1; i >= 0; i--) pending.push(node.childNodes[i]);\n }\n return found;\n }\n getAttribute(name) {\n return this.attributes !== undefined && hasOwn(this.attributes, name) ? this.attributes[name] : null;\n }\n hasAttribute(name) { return this.getAttribute(name) !== null; }\n // A document's text is the XML it was read from: what an <invoke>'s\n // content expression gives the system as the document to run.\n toString() { return this[SOURCE] !== undefined ? this[SOURCE] : Object.prototype.toString.call(this); }\n // As a neutral value (to_wire, JSON.stringify), a node is its XML text.\n toJSON() { return xmlText(this); }\n }\n defineProperty(XMLNode.prototype, XML, { value: true });\n\n // A node's XML: a document's the text it was read from, any other node's\n // written out.\n const escaped = (text, quote) => String(text).replaceAll('&', '&amp;').replaceAll('<', '&lt;')\n .replaceAll('>', '&gt;').replaceAll(quote ? '\"' : '\\0', quote ? '&quot;' : '\\0');\n function xmlText(node) {\n if (node[SOURCE] !== undefined) return node[SOURCE];\n const inner = () => node.childNodes.map(xmlText).join('');\n switch (node.nodeType) {\n case 1: {\n let text = '<' + node.nodeName;\n for (const key of keys(node.attributes ?? {})) text += ' ' + key + '=\"' + escaped(node.attributes[key], true) + '\"';\n return node.childNodes.length ? text + '>' + inner() + '</' + node.nodeName + '>' : text + '/>';\n }\n case 3: return escaped(node.nodeValue ?? '', false);\n case 4: return '<![CDATA[' + node.nodeValue + ']]>';\n case 7: return '<?' + node.nodeName + ' ' + (node.nodeValue ?? '') + '?>';\n case 8: return '<!--' + (node.nodeValue ?? '') + '-->';\n default: return inner();\n }\n }\n\n // A node of the tree C read with txml: [nodeType, nodeName, text, attributes, children].\n function node(entry) {\n const [type, name, text, attributes, children] = entry;\n const made = new XMLNode(type, name);\n if (type === 1) {\n made.tagName = name;\n const colon = name.indexOf(':');\n made.localName = colon < 0 ? name : name.slice(colon + 1);\n made.attributes = {};\n for (const key of keys(attributes)) own(made.attributes, key, attributes[key]);\n } else if (text !== undefined) made.nodeValue = text;\n for (const child of children) made.appendChild(node(child));\n return made;\n }\n\n function dom(children, text) {\n const document = new XMLNode(9, '#document');\n defineProperty(document, SOURCE, { value: text });\n for (const child of children) document.appendChild(node(child));\n return document;\n }\n\n function copy(value, depth) {\n if (typeof value === 'symbol')\n throw refuse('unsupported', new TypeError('a value crossing between actors is a primitive, a plain object, ' +\n 'an array, a Uint8Array or an XML node, not a symbol'));\n if (value === null || (typeof value !== 'object' && typeof value !== 'function')) return value;\n if (depth >= 64) throw refuse('invalid', new RangeError('a value nested deeper than 64 (or a cycle) does not cross between actors'));\n if (isArray(value)) {\n const made = [];\n for (let i = 0; i < value.length; i++) made.push(copy(value[i], depth + 1));\n return made;\n }\n if (typeof value === 'object') {\n if (isView(value) && tag.call(value) === '[object Uint8Array]') return new Uint8Array(value);\n // an application's Address (decode gives them): a chart has no reference type, so its text\n if (Address !== undefined && value instanceof Address) return value.text;\n if (value[XML] === true) {\n const made = new XMLNode(value.nodeType, value.nodeName);\n if (value[SOURCE] !== undefined) defineProperty(made, SOURCE, { value: value[SOURCE] });\n for (const key of keys(value)) if (key !== 'childNodes') own(made, key, copy(value[key], depth + 1));\n for (const child of value.childNodes) made.appendChild(copy(child, depth + 1));\n return made;\n }\n if (plain(getPrototypeOf(value))) {\n const made = {};\n for (const key of keys(value)) own(made, key, copy(value[key], depth + 1));\n return made;\n }\n }\n throw refuse('unsupported', new TypeError('a value crossing between actors is a primitive, a plain object, ' +\n 'an array, a Uint8Array or an XML node, not ' + tag.call(value)));\n }\n\n // A payload's value copied out to the application, in the realm this\n // script runs in (the application's own, for this): as copy, but an XML\n // node is its XML text.\n function exported(value, depth) {\n if (value === null || (typeof value !== 'object' && typeof value !== 'function')) return value;\n if (depth >= 64) throw new RangeError('a value nested deeper than 64 (or a cycle)');\n if (isArray(value)) {\n const made = [];\n for (let i = 0; i < value.length; i++) made.push(exported(value[i], depth + 1));\n return made;\n }\n if (typeof value === 'object') {\n if (isView(value) && tag.call(value) === '[object Uint8Array]') return new Uint8Array(value);\n if (value[XML] === true) return xmlText(value);\n const made = {};\n for (const key of keys(value)) own(made, key, exported(value[key], depth + 1));\n return made;\n }\n return value; // a function: kept as it is, as the payload held it\n }\n\n // _ioprocessors, made at its first read and kept in the link: {type:\n // {location}}, with the short names the W3C tests use. The link holds\n // the processors core gave ([type, location] pairs), or undefined when\n // they are the context's template with this session's id (system below).\n let template; // [prefix of the first location, the processors of the session that made it]\n function ioprocessors(link) {\n if (link.io !== undefined && !isArray(link.io)) return link.io;\n let processors = link.io;\n if (processors === undefined) {\n processors = template[1].slice();\n processors[0] = [processors[0][0], template[0] + link.sessionid];\n }\n const io = {};\n for (const [type, location] of processors) {\n const entry = freeze({ location });\n own(io, type, entry);\n if (type.endsWith('#SCXMLEventProcessor')) own(io, 'scxml', entry);\n if (type.endsWith('#BasicHTTPEventProcessor')) own(io, 'basichttp', entry);\n }\n link.io = freeze(io);\n return link.io;\n }\n\n // The system variables of the scopes, read through the session's link.\n const variables = freeze(create(null, {\n _sessionid: { get() { return this[LINK].sessionid; }, enumerable: true },\n _name: { get() { return this[LINK].name; }, enumerable: true },\n _ioprocessors: { get() { return ioprocessors(this[LINK]); }, enumerable: true },\n In: { value: In, enumerable: true },\n }));\n\n // A chart's constructor of scopes: the session's link, then every name the\n // chart declares, then _event, so every scope of the chart has one shape.\n function shape(names) {\n const declared = names.slice();\n function Scope(link) {\n this[LINK] = link;\n for (let i = 0; i < declared.length; i++) own(this, declared[i], undefined);\n defineProperty(this, '_event', { value: undefined, enumerable: true, configurable: true });\n }\n Scope.prototype = variables;\n return Scope;\n }\n\n // A session's link: its system variables and its params until its first\n // event. What sessions of a context have in common is kept once: the name\n // of the session made before (a chart's sessions mostly share one), and\n // the processors as a template whose first location ends with the\n // session's id, so a session whose processors match it, text for text,\n // keeps nothing of them until it reads _ioprocessors.\n let lastName;\n function same(processors, sessionid) {\n if (template === undefined) {\n const first = processors.length ? processors[0][1] : undefined;\n if (typeof first !== 'string' || !first.endsWith(sessionid)) return false;\n template = [first.slice(0, first.length - sessionid.length), processors];\n return true;\n }\n const kept = template[1];\n if (kept.length !== processors.length || processors[0][1] !== template[0] + sessionid) return false;\n for (let i = 0; i < kept.length; i++)\n if (kept[i][0] !== processors[i][0] || (i > 0 && kept[i][1] !== processors[i][1])) return false;\n return true;\n }\n function system(sessionid, name, processors) {\n if (name === lastName) name = lastName;\n else lastName = name;\n return { sessionid, name, io: same(processors, sessionid) ? undefined : processors, init: undefined };\n }\n\n // A context of its own session: the names on its global object.\n function declare(names, link) {\n const global = globalThis;\n global[LINK] = link;\n for (const name of names) if (!hasOwn(global, name)) own(global, name, undefined);\n defineProperty(global, '_sessionid', { get() { return link.sessionid; }, enumerable: true });\n defineProperty(global, '_name', { get() { return link.name; }, enumerable: true });\n defineProperty(global, '_ioprocessors', { get() { return ioprocessors(link); }, enumerable: true });\n defineProperty(global, 'In', { value: In, enumerable: true });\n defineProperty(global, '_event', { value: undefined, enumerable: true, configurable: true });\n return global;\n }\n\n function event(fields) {\n const [name, type, sendid, origin, origintype, invokeid, data, raw] = fields;\n const made = { name, type, sendid, origin, origintype, invokeid, data };\n if (raw !== undefined) made.raw = raw;\n return freeze(made);\n }\n\n // A session's variables, what hibernation writes: [names, values]. A\n // shared chart's scope: its own names but _event. A declaring session's\n // global: the names its chart and scripts put there (var, function, an\n // implicit global); one in `remade` (a function declaration of the\n // chart's scripts) is left out while it holds a function, since the\n // wake makes it again from the scripts.\n // A global's binding is read by its name, not as the global object's\n // property: bun's vm context leaves that property stale after a function\n // writes the var (spike, bun 1.4.1, 2026-10-16: `total += k` in a\n // function, globalThis.total still the old value, `total` the new one).\n function stored(target, remade) {\n const found = [];\n const values = [];\n const global = target === globalThis;\n for (const name of global ? names(target) : keys(target)) {\n if (name === '_event' || (global && (baseline.has(name) || systemNames.has(name)))) continue;\n const value = global && identifier.test(name) ? indirect(name) : target[name];\n if (global && typeof value === 'function' && remade.includes(name)) continue;\n found.push(name);\n values.push(value);\n }\n return [found, values];\n }\n\n // A woken session's variables, `vars` of a hibernation blob (made in\n // another realm), copied into this context: a shared scope keeps the\n // names its chart declares; a declaring session's global takes them all,\n // and its link keeps the names its chart did not declare, which upgrade\n // drops should the blob be of another version.\n function restore(target, vars) {\n const added = [];\n for (const name of keys(vars)) {\n if (hasOwn(target, name)) target[name] = copy(vars[name], 0);\n else if (target === globalThis) {\n own(target, name, copy(vars[name], 0));\n added.push(name);\n }\n }\n if (added.length) target[LINK].added = added;\n }\n\n // A woken session migrated to a new version of its chart: the old\n // bindings, the blob's variables its link kept since the wake, taken\n // from it; on a declaring session's global the names the new chart did\n // not declare go.\n function upgrade(target) {\n const link = target[LINK];\n const old = link.init;\n link.init = undefined;\n if (link.added !== undefined) {\n for (const name of link.added) delete target[name];\n link.added = undefined;\n }\n return old;\n }\n\n // A session's link as a host snapshot holds it: [sessionid, name, its I/O\n // processors as [type, location] pairs, init, added]. The processors are\n // the template's with this session's id, the pairs the link kept, or,\n // once _ioprocessors was read, its entries (an alias, `scxml` or\n // `basichttp`, is the same frozen entry as the type before it).\n function linked(target) {\n const link = target[LINK];\n let processors = link.io;\n if (processors === undefined) {\n processors = template[1].slice();\n processors[0] = [processors[0][0], template[0] + link.sessionid];\n } else if (!isArray(processors)) {\n const seen = new Set();\n const pairs = [];\n for (const type of keys(processors)) {\n const entry = processors[type];\n if (seen.has(entry)) continue;\n seen.add(entry);\n pairs.push([type, entry.location]);\n }\n processors = pairs;\n }\n return [link.sessionid, link.name, processors, link.init, link.added];\n }\n\n // A restored session's link gets the names a snapshot recorded as added\n // (restore computed its own), or none.\n function relink(target, added) {\n target[LINK].added = isArray(added) ? added.slice() : undefined;\n }\n\n // In a scratch context after a chart's scripts were instantiated: the\n // names they declared as functions.\n function functions() {\n const found = [];\n for (const name of names(globalThis)) if (!baseline.has(name) && typeof globalThis[name] === 'function') found.push(name);\n return found;\n }\n\n // The global's names a chart's scripts made (globals), and the names\n // made again from them on a wake (remade): the functions they declare,\n // and the names a strict scratch context's global (`strict`, its\n // globals()) lacks: a sloppy block's function declarations (Annex B),\n // whose var a script in executable content fills each time it runs.\n function globals() {\n return names(globalThis).filter((name) => !baseline.has(name));\n }\n function remade(strict) {\n const kept = new Set(strict);\n const found = functions();\n for (const name of globals()) if (!kept.has(name) && !found.includes(name)) found.push(name);\n return found;\n }\n\n function record(names, values) {\n const made = {};\n for (let i = 0; i < names.length; i++) own(made, names[i], copy(values[i], 1));\n return made;\n }\n\n return freeze({\n LINK,\n global: globalThis,\n objectPrototype: Object.prototype,\n copy: (value) => copy(value, 0),\n exported: (value) => exported(value, 0),\n refusal: (error) => refusals.get(error),\n shape,\n system,\n declare,\n event,\n record,\n stored,\n restore,\n upgrade,\n linked,\n relink,\n functions,\n globals,\n remade,\n dom,\n parse: (text) => JSON.parse(text),\n });\n})\n";
2
+ export default "// The realm's script of the native \"ecmascript\" datamodel (AGENTS.md, \"The\n// native \"ecmascript\" datamodel\"; src/datamodel/realms.c): run once in every\n// node:vm context the datamodel makes (the host's one shared context), with\n// In() as its argument. It returns the context's kit, the functions that\n// make what must be made with that context's constructors: copies of values\n// crossing between actors, the scopes of a chart (one constructor per\n// chart, over a frozen prototype of null prototype holding the system\n// variables and the chart's helpers), the system variables of a session,\n// _event, and XML documents; and what a chart's scripts declare, asked of\n// the engine (the binding parses no JavaScript). It runs in the\n// application's realm too, once, for the copies the application reads and\n// the compile call's advice. The build embeds it in the addon\n// (mise/napi.rb); it is not shipped as a file.\n(function tinyactorsRealm(In, Address) {\n 'use strict';\n const LINK = Symbol('tinyactors.session');\n const XML = Symbol.for('tinyactors.xml');\n const SOURCE = Symbol.for('tinyactors.xml.source');\n const { create, defineProperty, freeze, getPrototypeOf, hasOwn, keys } = Object;\n const tag = Object.prototype.toString;\n const source = Function.prototype.toString;\n const isArray = Array.isArray;\n const isView = ArrayBuffer.isView;\n const Code = Function; // this context's: what it compiles is this context's\n\n // The errors copy throws for a value that cannot cross, by status: an\n // input refused with them throws a TinyactorsError of that status.\n const refusals = new WeakMap();\n function refuse(status, error) {\n refusals.set(error, status);\n return error;\n }\n\n function own(object, key, value) {\n defineProperty(object, key, { value, writable: true, enumerable: true, configurable: true });\n }\n\n function plain(prototype) {\n if (prototype === null) return true;\n if (getPrototypeOf(prototype) !== null || !hasOwn(prototype, 'constructor')) return false;\n const made = prototype.constructor;\n return typeof made === 'function' && made.prototype === prototype && made.name === 'Object' &&\n source.call(made).includes('[native code]');\n }\n\n class XMLNode {\n constructor(nodeType, nodeName) {\n this.nodeType = nodeType;\n this.nodeName = nodeName;\n this.childNodes = [];\n defineProperty(this, 'parentNode', { value: null, writable: true, configurable: true });\n }\n get firstChild() { return this.childNodes.length ? this.childNodes[0] : null; }\n get lastChild() { return this.childNodes.length ? this.childNodes[this.childNodes.length - 1] : null; }\n get documentElement() {\n if (this.nodeType !== 9) return undefined;\n for (const child of this.childNodes) if (child.nodeType === 1) return child;\n return null;\n }\n get textContent() {\n if (this.nodeType === 3 || this.nodeType === 4) return this.nodeValue;\n let text = '';\n for (const child of this.childNodes) if (child.nodeType !== 8 && child.nodeType !== 7) text += child.textContent;\n return text;\n }\n appendChild(child) {\n child.parentNode = this;\n this.childNodes.push(child);\n return child;\n }\n getElementsByTagName(name) {\n const found = [];\n const pending = this.childNodes.slice().reverse();\n while (pending.length) {\n const node = pending.pop();\n if (node.nodeType !== 1) continue;\n if (name === '*' || node.nodeName === name || node.localName === name) found.push(node);\n for (let i = node.childNodes.length - 1; i >= 0; i--) pending.push(node.childNodes[i]);\n }\n return found;\n }\n getAttribute(name) {\n return this.attributes !== undefined && hasOwn(this.attributes, name) ? this.attributes[name] : null;\n }\n hasAttribute(name) { return this.getAttribute(name) !== null; }\n // A document's text is the XML it was read from: what an <invoke>'s\n // content expression gives the system as the document to run.\n toString() { return this[SOURCE] !== undefined ? this[SOURCE] : Object.prototype.toString.call(this); }\n // As a neutral value (to_wire, JSON.stringify), a node is its XML text.\n toJSON() { return xmlText(this); }\n }\n defineProperty(XMLNode.prototype, XML, { value: true });\n\n // A node's XML: a document's the text it was read from, any other node's\n // written out.\n const escaped = (text, quote) => String(text).replaceAll('&', '&amp;').replaceAll('<', '&lt;')\n .replaceAll('>', '&gt;').replaceAll(quote ? '\"' : '\\0', quote ? '&quot;' : '\\0');\n function xmlText(node) {\n if (node[SOURCE] !== undefined) return node[SOURCE];\n const inner = () => node.childNodes.map(xmlText).join('');\n switch (node.nodeType) {\n case 1: {\n let text = '<' + node.nodeName;\n for (const key of keys(node.attributes ?? {})) text += ' ' + key + '=\"' + escaped(node.attributes[key], true) + '\"';\n return node.childNodes.length ? text + '>' + inner() + '</' + node.nodeName + '>' : text + '/>';\n }\n case 3: return escaped(node.nodeValue ?? '', false);\n case 4: return '<![CDATA[' + node.nodeValue + ']]>';\n case 7: return '<?' + node.nodeName + ' ' + (node.nodeValue ?? '') + '?>';\n case 8: return '<!--' + (node.nodeValue ?? '') + '-->';\n default: return inner();\n }\n }\n\n // A node of the tree C read with txml: [nodeType, nodeName, text, attributes, children].\n function node(entry) {\n const [type, name, text, attributes, children] = entry;\n const made = new XMLNode(type, name);\n if (type === 1) {\n made.tagName = name;\n const colon = name.indexOf(':');\n made.localName = colon < 0 ? name : name.slice(colon + 1);\n made.attributes = {};\n for (const key of keys(attributes)) own(made.attributes, key, attributes[key]);\n } else if (text !== undefined) made.nodeValue = text;\n for (const child of children) made.appendChild(node(child));\n return made;\n }\n\n function dom(children, text) {\n const document = new XMLNode(9, '#document');\n defineProperty(document, SOURCE, { value: text });\n for (const child of children) document.appendChild(node(child));\n return document;\n }\n\n function copy(value, depth) {\n if (typeof value === 'symbol')\n throw refuse('unsupported', new TypeError('a value crossing between actors is a primitive, a plain object, ' +\n 'an array, a Uint8Array or an XML node, not a symbol'));\n if (value === null || (typeof value !== 'object' && typeof value !== 'function')) return value;\n if (depth >= 64) throw refuse('invalid', new RangeError('a value nested deeper than 64 (or a cycle) does not cross between actors'));\n if (isArray(value)) {\n const made = [];\n for (let i = 0; i < value.length; i++) made.push(copy(value[i], depth + 1));\n return made;\n }\n if (typeof value === 'object') {\n if (isView(value) && tag.call(value) === '[object Uint8Array]') return new Uint8Array(value);\n // an application's Address (decode gives them): a chart has no reference type, so its text\n if (Address !== undefined && value instanceof Address) return value.text;\n if (value[XML] === true) {\n const made = new XMLNode(value.nodeType, value.nodeName);\n if (value[SOURCE] !== undefined) defineProperty(made, SOURCE, { value: value[SOURCE] });\n for (const key of keys(value)) if (key !== 'childNodes') own(made, key, copy(value[key], depth + 1));\n for (const child of value.childNodes) made.appendChild(copy(child, depth + 1));\n return made;\n }\n if (plain(getPrototypeOf(value))) {\n const made = {};\n for (const key of keys(value)) own(made, key, copy(value[key], depth + 1));\n return made;\n }\n }\n throw refuse('unsupported', new TypeError('a value crossing between actors is a primitive, a plain object, ' +\n 'an array, a Uint8Array or an XML node, not ' + tag.call(value)));\n }\n\n // A payload's value copied out to the application, in the realm this\n // script runs in (the application's own, for this): as copy, but an XML\n // node is its XML text.\n function exported(value, depth) {\n if (value === null || (typeof value !== 'object' && typeof value !== 'function')) return value;\n if (depth >= 64) throw new RangeError('a value nested deeper than 64 (or a cycle)');\n if (isArray(value)) {\n const made = [];\n for (let i = 0; i < value.length; i++) made.push(exported(value[i], depth + 1));\n return made;\n }\n if (typeof value === 'object') {\n if (isView(value) && tag.call(value) === '[object Uint8Array]') return new Uint8Array(value);\n if (value[XML] === true) return xmlText(value);\n const made = {};\n for (const key of keys(value)) own(made, key, exported(value[key], depth + 1));\n return made;\n }\n return value; // a function: kept as it is, as the payload held it\n }\n\n // _ioprocessors, made at its first read and kept in the link: {type:\n // {location}}, with the short names the W3C tests use. The link holds\n // the processors core gave ([type, location] pairs), or undefined when\n // they are the context's template with this session's id (system below).\n let template; // [prefix of the first location, the processors of the session that made it]\n function ioprocessors(link) {\n if (link.io !== undefined && !isArray(link.io)) return link.io;\n let processors = link.io;\n if (processors === undefined) {\n processors = template[1].slice();\n processors[0] = [processors[0][0], template[0] + link.sessionid];\n }\n const io = {};\n for (const [type, location] of processors) {\n const entry = freeze({ location });\n own(io, type, entry);\n if (type.endsWith('#SCXMLEventProcessor')) own(io, 'scxml', entry);\n if (type.endsWith('#BasicHTTPEventProcessor')) own(io, 'basichttp', entry);\n }\n link.io = freeze(io);\n return link.io;\n }\n\n // The system variables of the scopes, read through the session's link.\n const variables = freeze(create(null, {\n _sessionid: { get() { return this[LINK].sessionid; }, enumerable: true },\n _name: { get() { return this[LINK].name; }, enumerable: true },\n _ioprocessors: { get() { return ioprocessors(this[LINK]); }, enumerable: true },\n In: { value: In, enumerable: true },\n }));\n\n // ---- what a script declares ----------------------------------------------\n\n // Whether `body` parses as a function body of this context, nothing run.\n function parses(body) {\n try {\n Code(body); // eslint-disable-line no-new-func\n return true;\n } catch (error) {\n if (error instanceof SyntaxError) return false;\n throw error;\n }\n }\n\n // What may be an identifier in a script's text: the candidates the\n // engine is asked about.\n const candidates = /[\\p{ID_Start}$_][\\p{ID_Continue}$\\u200c\\u200d]*/gu;\n const declaring = /\\b(?:var|let|const|class|function)\\b/;\n\n // The names a script declares at its top level, asked of the engine:\n // [functions, vars, lexicals] (let, const and class), each in the order\n // of the text; null when the script does not parse as the strict\n // function body the datamodel runs it as. A candidate is declared when\n // the script followed by `let <name>` does not parse, lexically when the\n // script followed by `var <name>` does not either; a declared var holds\n // a function before any statement runs when it is a function\n // declaration.\n function declarations(source) {\n const body = \"'use strict';\\n\" + source;\n if (!parses(body)) return null;\n const functions = [], vars = [], lexicals = [];\n if (!declaring.test(source)) return [functions, vars, lexicals];\n const seen = new Set(), hoisted = [];\n for (const [name] of source.matchAll(candidates)) {\n if (seen.has(name)) continue;\n seen.add(name);\n if (!parses(\"'use strict';let \" + name + ';') || parses(body + '\\n;let ' + name + ';')) continue;\n if (!parses(body + '\\n;var ' + name + ';')) lexicals.push(name);\n else hoisted.push(name);\n }\n if (hoisted.length) {\n const kinds = Code(\"'use strict';return [\" + hoisted.map((name) => 'typeof ' + name).join(',') + '];\\n' + source)();\n for (let i = 0; i < hoisted.length; i++) (kinds[i] === 'function' ? functions : vars).push(hoisted[i]);\n }\n return [functions, vars, lexicals];\n }\n\n const advised = (name) => 'declare it with <data id=\"' + name + '\"/> instead';\n\n // Why the native datamodel refuses a script: its first top-level let,\n // const or class declaration; undefined when it refuses none.\n function refusal(found) {\n if (found === null || !found[2].length) return undefined;\n return 'a <script> declares ' + found[2][0] + ' with let, const or class at its top level, which the native ' +\n '\"ecmascript\" datamodel refuses (the script is error.execution): ' + advised(found[2][0]);\n }\n\n // What the compile call warns of in a script: a refusal, or its first\n // top-level var, which the native datamodel makes a variable of the\n // session; null for neither.\n function advice(source) {\n const found = declarations(source);\n const refused = refusal(found);\n if (refused !== undefined) return refused;\n if (found === null || !found[1].length) return null;\n return 'a <script> declares var ' + found[1][0] + ' at its top level, which the native \"ecmascript\" datamodel ' +\n 'makes a variable of the session: ' + advised(found[1][0]);\n }\n\n // A script as the datamodel runs it, a function of the session's scope:\n // [functions, vars, refusal, head, tail], the head and tail around its\n // source; null when it does not parse (its evaluation's SyntaxError). Its\n // top-level vars are the session's variables: read from the scope before\n // it runs and written back after, as global code would keep them.\n function script(source) {\n const found = declarations(source);\n if (found === null) return null;\n const [functions, vars] = found;\n let head = \"(function(__s){with(__s){(function(){'use strict';\", tail = '\\n';\n if (vars.length) {\n head += vars.map((name) => name + '=__s.' + name + ';').join('') + 'try{';\n tail += '}finally{' + vars.map((name) => '__s.' + name + '=' + name + ';').join('') + '}';\n }\n return [functions, vars, refusal(found), head + '\\n', tail + '})();}})'];\n }\n\n // A chart's constructor of scopes: the session's link, then every name the\n // chart declares, then _event, so every scope of the chart has one shape.\n // Its prototype holds the system variables and, when its chart-level\n // scripts declare functions, those: made once per chart from each\n // script's text ([source, function names] in `scripts`), no statement of\n // it run, so they close over this context's globals and nothing of a\n // session's.\n function shape(names, scripts) {\n const declared = [...new Set(names)];\n let prototype = variables;\n if (scripts.length) {\n const helpers = {};\n for (const [source, functions] of scripts) {\n const made = Code(\"'use strict';return {\" + functions.join(',') + '};\\n' + source)();\n for (const name of functions) helpers[name] = { value: made[name], enumerable: true };\n }\n prototype = freeze(create(variables, helpers));\n }\n function Scope(link) {\n this[LINK] = link;\n for (let i = 0; i < declared.length; i++) own(this, declared[i], undefined);\n defineProperty(this, '_event', { value: undefined, enumerable: true, configurable: true });\n }\n Scope.prototype = prototype;\n return Scope;\n }\n\n // A session's link: its system variables and its params until its first\n // event. What sessions of a context have in common is kept once: the name\n // of the session made before (a chart's sessions mostly share one), and\n // the processors as a template whose first location ends with the\n // session's id, so a session whose processors match it, text for text,\n // keeps nothing of them until it reads _ioprocessors.\n let lastName;\n function same(processors, sessionid) {\n if (template === undefined) {\n const first = processors.length ? processors[0][1] : undefined;\n if (typeof first !== 'string' || !first.endsWith(sessionid)) return false;\n template = [first.slice(0, first.length - sessionid.length), processors];\n return true;\n }\n const kept = template[1];\n if (kept.length !== processors.length || processors[0][1] !== template[0] + sessionid) return false;\n for (let i = 0; i < kept.length; i++)\n if (kept[i][0] !== processors[i][0] || (i > 0 && kept[i][1] !== processors[i][1])) return false;\n return true;\n }\n function system(sessionid, name, processors) {\n if (name === lastName) name = lastName;\n else lastName = name;\n return { sessionid, name, io: same(processors, sessionid) ? undefined : processors, init: undefined };\n }\n\n function event(fields) {\n const [name, type, sendid, origin, origintype, invokeid, data, raw] = fields;\n const made = { name, type, sendid, origin, origintype, invokeid, data };\n if (raw !== undefined) made.raw = raw;\n return freeze(made);\n }\n\n // A session's variables, what hibernation writes: [names, values], its\n // scope's own names but _event.\n function stored(target) {\n const found = [];\n const values = [];\n for (const name of keys(target)) {\n if (name === '_event') continue;\n found.push(name);\n values.push(target[name]);\n }\n return [found, values];\n }\n\n // A woken session's variables, `vars` of a hibernation blob (made in\n // another realm), copied into this context: the scope keeps the names its\n // chart declares.\n function restore(target, vars) {\n for (const name of keys(vars)) if (hasOwn(target, name)) target[name] = copy(vars[name], 0);\n }\n\n // A woken session migrated to a new version of its chart: the old\n // bindings, the blob's variables its link kept since the wake, taken\n // from it.\n function upgrade(target) {\n const link = target[LINK];\n const old = link.init;\n link.init = undefined;\n return old;\n }\n\n // A session's link as a host snapshot holds it: [sessionid, name, its I/O\n // processors as [type, location] pairs, init]. The processors are the\n // template's with this session's id, the pairs the link kept, or, once\n // _ioprocessors was read, its entries (an alias, `scxml` or `basichttp`,\n // is the same frozen entry as the type before it).\n function linked(target) {\n const link = target[LINK];\n let processors = link.io;\n if (processors === undefined) {\n processors = template[1].slice();\n processors[0] = [processors[0][0], template[0] + link.sessionid];\n } else if (!isArray(processors)) {\n const seen = new Set();\n const pairs = [];\n for (const type of keys(processors)) {\n const entry = processors[type];\n if (seen.has(entry)) continue;\n seen.add(entry);\n pairs.push([type, entry.location]);\n }\n processors = pairs;\n }\n return [link.sessionid, link.name, processors, link.init];\n }\n\n function record(names, values) {\n const made = {};\n for (let i = 0; i < names.length; i++) own(made, names[i], copy(values[i], 1));\n return made;\n }\n\n return freeze({\n LINK,\n objectPrototype: Object.prototype,\n copy: (value) => copy(value, 0),\n exported: (value) => exported(value, 0),\n refusal: (error) => refusals.get(error),\n shape,\n script,\n advice,\n system,\n event,\n record,\n stored,\n restore,\n upgrade,\n linked,\n dom,\n parse: (text) => JSON.parse(text),\n });\n})\n";
@@ -0,0 +1,5 @@
1
+ // The version of the package installed: package.json's, written here by the
2
+ // release (mise/tasks/release) beside package.json's, and into each packed
3
+ // copy by mise/pack.rb (a development build's version too). A module, not
4
+ // a JSON import, so the package needs no JSON modules in a browser.
5
+ export const version = '0.4.0';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tinyactors/wasm",
3
- "version": "0.3.0",
3
+ "version": "0.4.0",
4
4
  "description": "SCXML statecharts as actors on the tinyactors host, in the browser, Node.js and Bun (WebAssembly; the API of @tinyactors/node)",
5
5
  "keywords": [
6
6
  "actor",
package/tinyactors.wasm CHANGED
Binary file