functionalscript 0.0.562 → 0.0.564
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/doc/byte-code.md +60 -0
- package/html/module.f.cjs +54 -69
- package/html/test.f.cjs +20 -0
- package/package.json +3 -3
package/doc/byte-code.md
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
# Byte Code
|
|
2
|
+
|
|
3
|
+
This format is designed for fast and straightforward serialization and doesn't depend on a particular VM implementation.
|
|
4
|
+
|
|
5
|
+
**Requirements:**
|
|
6
|
+
- VM serializer/deserializer should be very simple.
|
|
7
|
+
- string: UTF16
|
|
8
|
+
- number: in a binary format
|
|
9
|
+
- bigint: in a binary format
|
|
10
|
+
- len: u32
|
|
11
|
+
- the byte code doesn't know anything about importing modules or I/O functions.
|
|
12
|
+
- the byte code shouldn't contain syntax sugar.
|
|
13
|
+
- serialized in a byte array so we can save it into a file. One byte is one unit.
|
|
14
|
+
- least-significant byte first.
|
|
15
|
+
|
|
16
|
+
```rust
|
|
17
|
+
struct Array<T> {
|
|
18
|
+
len: u32,
|
|
19
|
+
array: [T; self.len],
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
type String = Array<u16>;
|
|
23
|
+
|
|
24
|
+
// LSB first.
|
|
25
|
+
type BigUInt = Array<u64>;
|
|
26
|
+
|
|
27
|
+
type Object = Array<(String, Any)>;
|
|
28
|
+
|
|
29
|
+
// This is the main structure for serialization.
|
|
30
|
+
type Code = Array<u8>;
|
|
31
|
+
|
|
32
|
+
struct Function {
|
|
33
|
+
length: u32
|
|
34
|
+
code: Code
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// This structure is not for serialization because
|
|
38
|
+
// a serialized module should resolve all imports.
|
|
39
|
+
struct Module {
|
|
40
|
+
import: Array<String>
|
|
41
|
+
code: Code
|
|
42
|
+
}
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
|type|any |tag| | |
|
|
46
|
+
|----|--------------|---|-----------------------|-----------------------------|
|
|
47
|
+
|JSON|null | 00| | |
|
|
48
|
+
| |number | 01|u64 | |
|
|
49
|
+
| |false | 02| | |
|
|
50
|
+
| |true | 03| | |
|
|
51
|
+
| |string | 04|String | |
|
|
52
|
+
| |object | 05|Object | |
|
|
53
|
+
| |array | 06|Array<Any> | |
|
|
54
|
+
|DJS |bigint+ | 07|BigUInt | |
|
|
55
|
+
| |bigint- | 08|BigUInt | |
|
|
56
|
+
| |local_ref | 09|u32 |consts[i] |
|
|
57
|
+
|FJS |arg_ref | 0A|u32 |args[i] |
|
|
58
|
+
| |undefined | 0B| | |
|
|
59
|
+
| |function | 0C|Function |the last constant is a return|
|
|
60
|
+
| |... | | | |
|
package/html/module.f.cjs
CHANGED
|
@@ -7,63 +7,38 @@ const { stringToList } = require('../text/utf16/module.f.cjs')
|
|
|
7
7
|
const { fromCharCode } = String
|
|
8
8
|
const { entries } = Object
|
|
9
9
|
|
|
10
|
-
/**
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
*/
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* https://developer.mozilla.org/en-US/docs/Glossary/Void_element
|
|
39
|
-
*
|
|
40
|
-
* @typedef {
|
|
41
|
-
* | 'area'
|
|
42
|
-
* | 'base'
|
|
43
|
-
* | 'br'
|
|
44
|
-
* | 'col'
|
|
45
|
-
* | 'embed'
|
|
46
|
-
* | 'hr'
|
|
47
|
-
* | 'img'
|
|
48
|
-
* | 'input'
|
|
49
|
-
* | 'link'
|
|
50
|
-
* | 'meta'
|
|
51
|
-
* | 'param'
|
|
52
|
-
* | 'source'
|
|
53
|
-
* | 'track'
|
|
54
|
-
* | 'wbr'
|
|
55
|
-
* } VoidTag
|
|
56
|
-
*/
|
|
57
|
-
|
|
58
|
-
/** @typedef {readonly[VoidTag]} VoidElement1*/
|
|
59
|
-
|
|
60
|
-
/** @typedef {readonly[VoidTag, Attributes]} VoidElement2 */
|
|
61
|
-
|
|
62
|
-
/** @typedef {readonly[Tag, readonly Node[]]} Element2 */
|
|
10
|
+
/** @typedef {string} Tag */
|
|
11
|
+
|
|
12
|
+
// https://developer.mozilla.org/en-US/docs/Glossary/Void_element
|
|
13
|
+
const voidTagList = [
|
|
14
|
+
'area',
|
|
15
|
+
'base',
|
|
16
|
+
'br',
|
|
17
|
+
'col',
|
|
18
|
+
'embed',
|
|
19
|
+
'hr',
|
|
20
|
+
'img',
|
|
21
|
+
'input',
|
|
22
|
+
'link',
|
|
23
|
+
'meta',
|
|
24
|
+
'param',
|
|
25
|
+
'source',
|
|
26
|
+
'track',
|
|
27
|
+
'wbr',
|
|
28
|
+
]
|
|
29
|
+
|
|
30
|
+
/** @type {(tag: string) => boolean} */
|
|
31
|
+
const isVoid = tag => voidTagList.includes(tag)
|
|
32
|
+
|
|
33
|
+
/** @typedef {readonly[Tag]} Element1*/
|
|
34
|
+
|
|
35
|
+
/** @typedef {readonly[Tag, Attributes]} Element2A */
|
|
36
|
+
|
|
37
|
+
/** @typedef {readonly[Tag, readonly Node[]]} Element2N */
|
|
63
38
|
|
|
64
39
|
/** @typedef {readonly[Tag, Attributes, Nodes]} Element3*/
|
|
65
40
|
|
|
66
|
-
/** @typedef {
|
|
41
|
+
/** @typedef {Element1 | Element2A | Element2N | Element3} Element */
|
|
67
42
|
|
|
68
43
|
/**
|
|
69
44
|
* @typedef {{
|
|
@@ -103,29 +78,39 @@ const attribute = ([name, value]) => flat([[' ', name, '="'], escape(value), ['"
|
|
|
103
78
|
/** @type {(a: Attributes) => list.List<string>} */
|
|
104
79
|
const attributes = compose(entries)(flatMap(attribute))
|
|
105
80
|
|
|
81
|
+
const open = (/** @type {Element2A} */[tag, a]) => flat([[`<`, tag], attributes(a), [`>`]])
|
|
82
|
+
|
|
83
|
+
const close = (/** @type {string}*/tag) => ['</', tag, '>']
|
|
84
|
+
|
|
85
|
+
/** @type {(_: Element3) => list.List<string>} */
|
|
86
|
+
const element3 = ([tag, a, ns]) =>
|
|
87
|
+
flat([open([tag, a]), nodes(ns), close(tag)])
|
|
88
|
+
|
|
89
|
+
/** @type {(_: Element2A) => list.List<string>} */
|
|
90
|
+
const element2a = e => {
|
|
91
|
+
const [tag] = e
|
|
92
|
+
return flat([open(e), isVoid(tag) ? [] : close(tag)])
|
|
93
|
+
}
|
|
94
|
+
|
|
106
95
|
/** @type {(element: Element) => list.List<string>} */
|
|
107
96
|
const element = e => {
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
const [tag, a, ns] = e
|
|
119
|
-
return [['<', tag], attributes(a), ['>'], nodes(ns), ['</', tag, '>']]
|
|
120
|
-
}
|
|
97
|
+
switch (e.length) {
|
|
98
|
+
case 1: { return element2a([e[0], {}]) }
|
|
99
|
+
case 2: {
|
|
100
|
+
const [tag, a] = e
|
|
101
|
+
return a instanceof Array ?
|
|
102
|
+
element3([tag, {}, a]) :
|
|
103
|
+
element2a([tag, a])
|
|
104
|
+
}
|
|
105
|
+
default: {
|
|
106
|
+
return element3(e)
|
|
121
107
|
}
|
|
122
108
|
}
|
|
123
|
-
return flat(f())
|
|
124
109
|
}
|
|
125
110
|
|
|
126
111
|
const html = compose(element)(listConcat(['<!DOCTYPE html>']))
|
|
127
112
|
|
|
128
|
-
const htmlToString =
|
|
113
|
+
const htmlToString = compose(html)(stringConcat)
|
|
129
114
|
|
|
130
115
|
module.exports = {
|
|
131
116
|
/** @readonly */
|
package/html/test.f.cjs
CHANGED
|
@@ -5,10 +5,30 @@ module.exports = {
|
|
|
5
5
|
const r = _.htmlToString(['html', []])
|
|
6
6
|
if (r !== '<!DOCTYPE html><html></html>') { throw r }
|
|
7
7
|
},
|
|
8
|
+
empty2: () => {
|
|
9
|
+
const r = _.htmlToString(['html'])
|
|
10
|
+
if (r !== '<!DOCTYPE html><html></html>') { throw r }
|
|
11
|
+
},
|
|
12
|
+
void: () => {
|
|
13
|
+
const r = _.htmlToString(['area'])
|
|
14
|
+
if (r !== '<!DOCTYPE html><area>') { throw r }
|
|
15
|
+
},
|
|
8
16
|
some: () => {
|
|
9
17
|
/** @type {_.Element} */
|
|
10
18
|
const x = ['div', {}, ['<div>&</div>', ['a', { href: 'hello"' }, []]]]
|
|
11
19
|
const s = _.htmlToString(x)
|
|
12
20
|
if (s !== '<!DOCTYPE html><div><div>&amp;</div><a href="hello""></a></div>') { throw s }
|
|
21
|
+
},
|
|
22
|
+
some2: () => {
|
|
23
|
+
/** @type {_.Element} */
|
|
24
|
+
const x = ['div', ['<div>&</div>', ['a', { href: 'hello"' }, []]]]
|
|
25
|
+
const s = _.htmlToString(x)
|
|
26
|
+
if (s !== '<!DOCTYPE html><div><div>&amp;</div><a href="hello""></a></div>') { throw s }
|
|
27
|
+
},
|
|
28
|
+
someVoid: () => {
|
|
29
|
+
/** @type {_.Element} */
|
|
30
|
+
const x = ['div', [['br', {id: '5'}], '<div>&</div>', ['a', { href: 'hello"' }, []]]]
|
|
31
|
+
const s = _.htmlToString(x)
|
|
32
|
+
if (s !== '<!DOCTYPE html><div><br id="5"><div>&amp;</div><a href="hello""></a></div>') { throw s }
|
|
13
33
|
}
|
|
14
34
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "functionalscript",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.564",
|
|
4
4
|
"description": "FunctionalScript is a functional subset of JavaScript",
|
|
5
5
|
"main": "module.f.cjs",
|
|
6
6
|
"scripts": {
|
|
@@ -30,7 +30,7 @@
|
|
|
30
30
|
},
|
|
31
31
|
"homepage": "https://github.com/functionalscript/functionalscript#readme",
|
|
32
32
|
"devDependencies": {
|
|
33
|
-
"@types/node": "^22.
|
|
34
|
-
"typescript": "^5.
|
|
33
|
+
"@types/node": "^22.9.0",
|
|
34
|
+
"typescript": "^5.6.3"
|
|
35
35
|
}
|
|
36
36
|
}
|