porffor 0.2.0-fdf0fc5 → 0.14.0-0d97d1e6a
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/README.md +9 -2
- package/byg/index.js +3 -24
- package/compiler/2c.js +2 -53
- package/compiler/assemble.js +37 -11
- package/compiler/builtins/annexb_string.js +10 -10
- package/compiler/builtins/annexb_string.ts +3 -3
- package/compiler/builtins/array.ts +2 -6
- package/compiler/builtins/base64.ts +1 -1
- package/compiler/builtins/boolean.ts +1 -3
- package/compiler/builtins/crypto.ts +1 -1
- package/compiler/builtins/date.ts +1 -4
- package/compiler/builtins/escape.ts +1 -1
- package/compiler/builtins/function.ts +0 -2
- package/compiler/builtins/int.ts +0 -2
- package/compiler/builtins/number.ts +3 -8
- package/compiler/builtins/object.ts +0 -2
- package/compiler/builtins/porffor.d.ts +9 -8
- package/compiler/builtins/set.ts +184 -2
- package/compiler/builtins/string.ts +1 -1
- package/compiler/builtins/symbol.ts +61 -0
- package/compiler/builtins.js +8 -9
- package/compiler/codegen.js +121 -124
- package/compiler/decompile.js +3 -3
- package/compiler/embedding.js +2 -2
- package/compiler/encoding.js +0 -14
- package/compiler/expression.js +1 -1
- package/compiler/generated_builtins.js +348 -204
- package/compiler/index.js +3 -10
- package/compiler/opt.js +7 -7
- package/compiler/parse.js +1 -3
- package/compiler/precompile.js +17 -25
- package/compiler/prefs.js +6 -2
- package/compiler/prototype.js +5 -5
- package/compiler/wasmSpec.js +5 -0
- package/compiler/wrap.js +88 -57
- package/package.json +1 -1
- package/runner/compare.js +0 -1
- package/runner/debug.js +1 -6
- package/runner/profiler.js +15 -42
- package/runner/repl.js +3 -9
- package/runner/sizes.js +2 -2
- package/runner/version.js +10 -8
package/compiler/builtins/set.ts
CHANGED
@@ -1,5 +1,187 @@
|
|
1
|
-
//
|
1
|
+
// dark wasm magic for dealing with memory, sorry.
|
2
|
+
export const __Porffor_allocate = (): number => {
|
3
|
+
Porffor.wasm`i32.const 1
|
4
|
+
memory.grow 0
|
5
|
+
drop
|
6
|
+
memory.size 0
|
7
|
+
i32.const 1
|
8
|
+
i32.sub
|
9
|
+
i32.const 65536
|
10
|
+
i32.mul
|
11
|
+
i32.from_u
|
12
|
+
return`;
|
13
|
+
};
|
2
14
|
|
3
|
-
export const
|
15
|
+
export const __Porffor_set_read = (_this: Set, index: number): any => {
|
16
|
+
Porffor.wasm`
|
17
|
+
local offset i32
|
18
|
+
local.get ${index}
|
19
|
+
i32.to_u
|
20
|
+
i32.const 9
|
21
|
+
i32.mul
|
22
|
+
local.get ${_this}
|
23
|
+
i32.to_u
|
24
|
+
i32.add
|
25
|
+
local.set offset
|
4
26
|
|
27
|
+
local.get offset
|
28
|
+
f64.load 0 4
|
29
|
+
|
30
|
+
local.get offset
|
31
|
+
i32.load8_u 0 12
|
32
|
+
return`;
|
33
|
+
};
|
34
|
+
|
35
|
+
export const __Porffor_set_write = (_this: Set, index: number, value: any): boolean => {
|
36
|
+
Porffor.wasm`
|
37
|
+
local offset i32
|
38
|
+
local.get ${index}
|
39
|
+
i32.to_u
|
40
|
+
i32.const 9
|
41
|
+
i32.mul
|
42
|
+
local.get ${_this}
|
43
|
+
i32.to_u
|
44
|
+
i32.add
|
45
|
+
local.set offset
|
46
|
+
|
47
|
+
local.get offset
|
48
|
+
local.get ${value}
|
49
|
+
f64.store 0 4
|
50
|
+
|
51
|
+
local.get offset
|
52
|
+
local.get ${value+1}
|
53
|
+
i32.store8 0 12`;
|
54
|
+
|
55
|
+
return true;
|
56
|
+
};
|
57
|
+
|
58
|
+
|
59
|
+
// todo: this should be a getter somehow not a method
|
60
|
+
export const __Set_prototype_size = (_this: Set) => {
|
61
|
+
return Porffor.wasm.i32.load(_this, 0, 0);
|
62
|
+
};
|
63
|
+
|
64
|
+
export const __Set_prototype_values = (_this: Set) => {
|
65
|
+
// todo: this should return an iterator not array
|
66
|
+
const size: number = __Set_prototype_size(_this);
|
67
|
+
|
68
|
+
const out: any[] = __Porffor_allocate();
|
69
|
+
for (let i: number = 0; i < size; i++) {
|
70
|
+
const val: any = __Porffor_set_read(_this, i);
|
71
|
+
out.push(val);
|
72
|
+
}
|
73
|
+
|
74
|
+
return out;
|
75
|
+
};
|
76
|
+
|
77
|
+
export const __Set_prototype_keys = (_this: Set) => {
|
78
|
+
return __Set_prototype_values(_this);
|
79
|
+
};
|
80
|
+
|
81
|
+
export const __Set_prototype_has = (_this: Set, value: any) => {
|
82
|
+
const size: number = __Set_prototype_size(_this);
|
83
|
+
|
84
|
+
for (let i: number = 0; i < size; i++) {
|
85
|
+
if (__Porffor_set_read(_this, i) === value) return true;
|
86
|
+
}
|
87
|
+
|
88
|
+
return false;
|
89
|
+
};
|
90
|
+
|
91
|
+
export const __Set_prototype_add = (_this: Set, value: any) => {
|
92
|
+
const size: number = __Set_prototype_size(_this);
|
93
|
+
|
94
|
+
// check if already in set
|
95
|
+
for (let i: number = 0; i < size; i++) {
|
96
|
+
if (__Porffor_set_read(_this, i) === value) return _this;
|
97
|
+
}
|
98
|
+
|
99
|
+
// not, add it
|
100
|
+
// increment size by 1
|
101
|
+
Porffor.wasm.i32.store(_this, size + 1, 0, 0);
|
102
|
+
|
103
|
+
// write new value at end
|
104
|
+
__Porffor_set_write(_this, size, value);
|
105
|
+
|
106
|
+
return _this;
|
107
|
+
};
|
108
|
+
|
109
|
+
export const __Set_prototype_delete = (_this: Set, value: any) => {
|
110
|
+
const size: number = __Set_prototype_size(_this);
|
111
|
+
|
112
|
+
// check if already in set
|
113
|
+
for (let i: number = 0; i < size; i++) {
|
114
|
+
if (__Porffor_set_read(_this, i) === value) {
|
115
|
+
// found, delete
|
116
|
+
// decrement size by 1
|
117
|
+
Porffor.wasm.i32.store(_this, size - 1, 0, 0);
|
118
|
+
|
119
|
+
// offset all elements after by -1 ind
|
120
|
+
Porffor.wasm`
|
121
|
+
local offset i32
|
122
|
+
local.get ${i}
|
123
|
+
i32.to_u
|
124
|
+
i32.const 9
|
125
|
+
i32.mul
|
126
|
+
local.get ${_this}
|
127
|
+
i32.to_u
|
128
|
+
i32.add
|
129
|
+
i32.const 4
|
130
|
+
i32.add
|
131
|
+
local.set offset
|
132
|
+
|
133
|
+
;; dst = offset (this element)
|
134
|
+
local.get offset
|
135
|
+
|
136
|
+
;; src = offset + 9 (this element + 1 element)
|
137
|
+
local.get offset
|
138
|
+
i32.const 9
|
139
|
+
i32.add
|
140
|
+
|
141
|
+
;; size = (size - i - 1) * 9
|
142
|
+
local.get ${size}
|
143
|
+
local.get ${i}
|
144
|
+
f64.sub
|
145
|
+
i32.to_u
|
146
|
+
i32.const 1
|
147
|
+
i32.sub
|
148
|
+
i32.const 9
|
149
|
+
i32.mul
|
150
|
+
|
151
|
+
memory.copy 0 0`;
|
152
|
+
|
153
|
+
return true;
|
154
|
+
}
|
155
|
+
}
|
156
|
+
|
157
|
+
// not, return false
|
158
|
+
return false;
|
159
|
+
};
|
160
|
+
|
161
|
+
export const __Set_prototype_clear = (_this: Set) => {
|
162
|
+
// just set size to 0
|
163
|
+
// do not need to delete any as will not be accessed anymore,
|
164
|
+
// and will be overwritten with new add
|
165
|
+
Porffor.wasm.i32.store(_this, 0, 0, 0);
|
166
|
+
};
|
167
|
+
|
168
|
+
export const Set = () => {
|
169
|
+
throw new TypeError("Constructor Set requires 'new'");
|
170
|
+
};
|
171
|
+
|
172
|
+
export const Set$constructor = (iterable: any): Set => {
|
173
|
+
const out: Set = __Porffor_allocate();
|
174
|
+
|
175
|
+
const type: number = Porffor.rawType(iterable);
|
176
|
+
if (Porffor.fastOr(
|
177
|
+
type == Porffor.TYPES.array,
|
178
|
+
type == Porffor.TYPES.string, type == Porffor.TYPES.bytestring,
|
179
|
+
type == Porffor.TYPES.set
|
180
|
+
)) {
|
181
|
+
for (const x of iterable) {
|
182
|
+
__Set_prototype_add(out, x);
|
183
|
+
}
|
184
|
+
}
|
185
|
+
|
186
|
+
return out;
|
5
187
|
};
|
@@ -0,0 +1,61 @@
|
|
1
|
+
export const __Porffor_symbol_descStore = (op: boolean, value: any): any => {
|
2
|
+
const ptr: bytestring = '';
|
3
|
+
|
4
|
+
if (op) { // write
|
5
|
+
const size: number = Porffor.wasm.i32.load(ptr, 0, 0);
|
6
|
+
Porffor.wasm.i32.store(ptr, size + 1, 0, 0)
|
7
|
+
|
8
|
+
// reuse set internals to store description
|
9
|
+
__Porffor_set_write(ptr, size, value);
|
10
|
+
return size;
|
11
|
+
} else { // read
|
12
|
+
return __Porffor_set_read(ptr, value);
|
13
|
+
}
|
14
|
+
};
|
15
|
+
|
16
|
+
export const Symbol = (description: any): Symbol => {
|
17
|
+
// 1-based so always truthy as numeric value
|
18
|
+
return __Porffor_symbol_descStore(true, description) + 1;
|
19
|
+
};
|
20
|
+
|
21
|
+
// todo: this should be a getter somehow not a method
|
22
|
+
export const __Symbol_prototype_description = (_this: Symbol) => {
|
23
|
+
const description: bytestring = __Porffor_symbol_descStore(false,
|
24
|
+
Porffor.wasm`local.get ${_this}` - 1);
|
25
|
+
return description;
|
26
|
+
};
|
27
|
+
|
28
|
+
export const __Symbol_prototype_toString = (_this: Symbol) => {
|
29
|
+
let out: bytestring = '';
|
30
|
+
|
31
|
+
// Symbol(
|
32
|
+
Porffor.wasm.i32.store8(out, 83, 0, 4);
|
33
|
+
Porffor.wasm.i32.store8(out, 121, 0, 5);
|
34
|
+
Porffor.wasm.i32.store8(out, 109, 0, 6);
|
35
|
+
Porffor.wasm.i32.store8(out, 98, 0, 7);
|
36
|
+
Porffor.wasm.i32.store8(out, 111, 0, 8);
|
37
|
+
Porffor.wasm.i32.store8(out, 108, 0, 9);
|
38
|
+
Porffor.wasm.i32.store8(out, 40, 0, 10);
|
39
|
+
|
40
|
+
const description: bytestring = __Porffor_symbol_descStore(false,
|
41
|
+
Porffor.wasm`local.get ${_this}` - 1);
|
42
|
+
|
43
|
+
const descLen: i32 = description.length;
|
44
|
+
let outPtr: i32 = Porffor.wasm`local.get ${out}` + 7;
|
45
|
+
let descPtr: i32 = Porffor.wasm`local.get ${description}`;
|
46
|
+
const descPtrEnd: i32 = descPtr + descLen;
|
47
|
+
while (descPtr < descPtrEnd) {
|
48
|
+
Porffor.wasm.i32.store8(outPtr++, Porffor.wasm.i32.load8_u(descPtr++, 0, 4), 0, 4);
|
49
|
+
}
|
50
|
+
|
51
|
+
// )
|
52
|
+
Porffor.wasm.i32.store8(Porffor.wasm`local.get ${out}` + descLen, 41, 0, 11);
|
53
|
+
|
54
|
+
out.length = 8 + descLen;
|
55
|
+
|
56
|
+
return out;
|
57
|
+
};
|
58
|
+
|
59
|
+
export const __Symbol_prototype_valueOf = (_this: Symbol) => {
|
60
|
+
return _this;
|
61
|
+
};
|
package/compiler/builtins.js
CHANGED
@@ -1,8 +1,8 @@
|
|
1
|
-
import { Blocktype, Opcodes, Valtype, ValtypeSize } from "./wasmSpec.js";
|
2
|
-
import { number, i32x4 } from "./embedding.js";
|
3
|
-
import Prefs from './prefs.js';
|
4
1
|
import * as GeneratedBuiltins from './generated_builtins.js';
|
2
|
+
import { Blocktype, Opcodes, Valtype, ValtypeSize } from './wasmSpec.js';
|
3
|
+
import { number } from './embedding.js';
|
5
4
|
import { TYPES } from './types.js';
|
5
|
+
import Prefs from './prefs.js';
|
6
6
|
|
7
7
|
export const importedFuncs = [
|
8
8
|
{
|
@@ -48,8 +48,6 @@ for (let i = 0; i < importedFuncs.length; i++) {
|
|
48
48
|
importedFuncs[f.name] = i;
|
49
49
|
}
|
50
50
|
|
51
|
-
const char = c => number(c.charCodeAt(0));
|
52
|
-
|
53
51
|
const printStaticStr = str => {
|
54
52
|
const out = [];
|
55
53
|
|
@@ -140,7 +138,7 @@ export const BuiltinVars = function() {
|
|
140
138
|
this.Math = number(1);
|
141
139
|
|
142
140
|
// wintercg(tm)
|
143
|
-
this.__navigator_userAgent = (scope, { makeString }) => makeString(scope, `Porffor/0.
|
141
|
+
this.__navigator_userAgent = (scope, { makeString }) => makeString(scope, `Porffor/0.14.0`, false, '__navigator_userAgent');
|
144
142
|
this.__navigator_userAgent.type = Prefs.bytestring ? TYPES.bytestring : TYPES.string;
|
145
143
|
|
146
144
|
for (const x in TYPES) {
|
@@ -200,7 +198,8 @@ export const BuiltinFuncs = function() {
|
|
200
198
|
returns: [ valtypeBinary ],
|
201
199
|
wasm: [
|
202
200
|
[ Opcodes.local_get, 0 ]
|
203
|
-
]
|
201
|
+
],
|
202
|
+
constr: true
|
204
203
|
};
|
205
204
|
|
206
205
|
// just return given (default 0) for (new) Object() as we somewhat supports object just not constructor
|
@@ -212,7 +211,8 @@ export const BuiltinFuncs = function() {
|
|
212
211
|
wasm: [
|
213
212
|
// [ Opcodes.local_get, 0 ]
|
214
213
|
...number(1)
|
215
|
-
]
|
214
|
+
],
|
215
|
+
constr: true
|
216
216
|
};
|
217
217
|
|
218
218
|
|
@@ -569,7 +569,6 @@ export const BuiltinFuncs = function() {
|
|
569
569
|
|
570
570
|
// this is an implementation of xorshift128+ (in wasm bytecode)
|
571
571
|
// fun fact: v8, SM, JSC also use this (you will need this fun fact to maintain your sanity reading this code)
|
572
|
-
// const prngSeed0 = Math.floor(Math.random() * Number.MAX_SAFE_INTEGER), prngSeed1 = Math.floor(Math.random() * Number.MAX_SAFE_INTEGER);
|
573
572
|
const prngSeed0 = (Math.random() * (2 ** 30)) | 0, prngSeed1 = (Math.random() * (2 ** 30)) | 0;
|
574
573
|
|
575
574
|
const prng = ({
|