porffor 0.14.0-032e4ad08 → 0.14.0-0ad2c8a7c

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/CONTRIBUTING.md CHANGED
@@ -98,7 +98,7 @@ Loads the character code at the pointer `pointer` **for a String**.[^1]
98
98
  Porffor.wasm.i32.store(pointer, length, 0, 0)
99
99
  ```
100
100
 
101
- Stores the length `length` at pointer `pointer`, setting the length of an object. This is mostly unneeded today as you can just do `obj.length = length`. [^2]
101
+ Stores the length `length` at pointer `pointer`, setting the length of an object. This is mostly unneeded today as you can just do `obj.length = length`. (The `0, 4` args are necessary for the Wasm instruction, but you don't need to worry about them (`0` alignment, `0` byte offset).
102
102
 
103
103
  <br>
104
104
 
@@ -205,6 +205,7 @@ Store the character code into the `out` pointer variable, and increment it.
205
205
  - Do not set a return type for prototype methods, it can cause errors/unexpected results.
206
206
  - You cannot use other functions in the file not exported, or variables not inside the current function.
207
207
  - `if (...)` uses a fast truthy implementation which is not spec-compliant as most conditions should be strictly checked. To use spec-compliant behavior, use `if (Boolean(...))`.
208
+ - For object (string/array/etc) literals, you must use a variable eg `const out: bytestring = 'foobar'; console.log(out);` instead of `console.log('foobar')` due to precompile's allocator constraints.
208
209
 
209
210
  <br>
210
211
 
@@ -257,6 +258,4 @@ It will also log new passes/fails. Be careful as sometimes the overall passes ca
257
258
 
258
259
  <br>
259
260
 
260
- [^1]: The `0, 4` args are necessary for the Wasm instruction, but you don't need to worry about them (`0` alignment, `4` byte offset for length).
261
-
262
- [^2]: The `0, 4` args are necessary for the Wasm instruction, but you don't need to worry about them (`0` alignment, `0` byte offset).
261
+ [^1]: The `0, 4` args are necessary for the Wasm instruction, but you don't need to worry about them (`0` alignment, `4` byte offset for length).
package/README.md CHANGED
@@ -14,7 +14,7 @@ Porffor is primarily built from scratch, the only thing that is not is the parse
14
14
  Expect nothing to work! Only very limited JS is currently supported. See files in `bench` for examples.
15
15
 
16
16
  ### Install
17
- **`npm install -g porffor`**. It's that easy (hopefully) :)
17
+ **`npm install -g porffor@latest`**. It's that easy (hopefully) :)
18
18
 
19
19
  ### Trying a REPL
20
20
  **`porf`**. Just run it with no script file argument.
package/compiler/2c.js CHANGED
@@ -207,6 +207,8 @@ export default ({ funcs, globals, tags, data, exceptions, pages }) => {
207
207
  depth = 1;
208
208
  brDepth = 0;
209
209
 
210
+ let retTmpId = 0;
211
+
210
212
  const invLocals = inv(f.locals, x => x.idx);
211
213
 
212
214
  for (const x in invLocals) {
@@ -537,7 +539,7 @@ _time_out = _time.tv_nsec / 1000000. + _time.tv_sec * 1000.;`);
537
539
  if (func.internal) {
538
540
  vals.push(`${sanitize(func.name)}(${args.join(', ')})`);
539
541
  } else {
540
- line(`const struct ReturnValue _ = ${sanitize(func.name)}(${args.join(', ')})`);
542
+ line(`const struct ReturnValue _${retTmpId++} = ${sanitize(func.name)}(${args.join(', ')})`);
541
543
  vals.push(`_.value`);
542
544
  vals.push(`_.type`);
543
545
  }
@@ -0,0 +1,4 @@
1
+ export const __console_clear = (): void => {
2
+ const clear: bytestring = '\x1b[1;1H\x1b[J';
3
+ Porffor.print(clear);
4
+ };
@@ -1095,16 +1095,5 @@ export const BuiltinFuncs = function() {
1095
1095
  ]
1096
1096
  };
1097
1097
 
1098
-
1099
- this.__fs_readFileSync = {
1100
- params: [ valtypeBinary, valtypeBinary ],
1101
- locals: [],
1102
- returns: [ valtypeBinary ],
1103
- returnType: TYPES.bytestring,
1104
- wasm: [
1105
- [ Opcodes.call, importedFuncs.__Porffor_readFile ],
1106
- ]
1107
- };
1108
-
1109
1098
  GeneratedBuiltins.BuiltinFuncs.call(this);
1110
1099
  };
@@ -3473,19 +3473,8 @@ const withType = (scope, wasm, type) => [
3473
3473
 
3474
3474
  const generateMember = (scope, decl, _global, _name) => {
3475
3475
  const name = decl.object.name;
3476
-
3477
- // hack: process.argv[n]
3478
- if (name === '__process_argv') {
3479
- const setPointer = scope.arrays?.get(_name);
3480
-
3481
- return [
3482
- ...number(decl.property.value - 1),
3483
- ...(setPointer ? number(setPointer) : makeArray(scope, { elements: [] }, undefined, undefined, true, 'i8')[0]),
3484
- [ Opcodes.call, importedFuncs.__Porffor_readArgv ]
3485
- ];
3486
- }
3487
-
3488
3476
  const pointer = scope.arrays?.get(name);
3477
+
3489
3478
  const aotPointer = Prefs.aotPointerOpt && pointer;
3490
3479
 
3491
3480
  // hack: .name
@@ -89,6 +89,10 @@ export default (wasm, name = '', ind = 0, locals = {}, params = [], returns = []
89
89
  if (inst[0] === Opcodes.call || inst[0] === Opcodes.return_call) {
90
90
  const callFunc = funcs.find(x => x.index === inst[1]);
91
91
  if (callFunc) out += ` ;; $${callFunc.name} ${makeSignature(callFunc.params, callFunc.returns)}`;
92
+ if (globalThis.importFuncs && inst[1] < importFuncs.length) {
93
+ const importFunc = importFuncs[inst[1]];
94
+ out += ` ;; import ${importFunc.name} ${makeSignature(new Array(importFunc.params).fill(valtypeBinary), new Array(importFunc.returns).fill(valtypeBinary),)}`;
95
+ }
92
96
  }
93
97
 
94
98
  if (inst[0] === Opcodes.local_get || inst[0] === Opcodes.local_set || inst[0] === Opcodes.local_tee) {
@@ -407,6 +407,16 @@ export const BuiltinFuncs = function() {
407
407
  locals: [],
408
408
  localNames: ["_this","_this#type"],
409
409
  };
410
+ this.__console_clear = {
411
+ wasm: (scope, {allocPage,builtin,}) => [...number(allocPage(scope, 'bytestring: __console_clear/clear', 'i8') * pageSize, 124),[34,0],[65,18],[16, builtin('__Porffor_print')],[68,0,0,0,0,0,0,0,0],[65,3],[15]],
412
+ params: [],
413
+ typedParams: true,
414
+ returns: [124,127],
415
+ typedReturns: true,
416
+ locals: [124],
417
+ localNames: ["clear"],
418
+ data: [{"offset":0,"bytes":[9,0,0,0,27,91,49,59,49,72,27,91,74]}],
419
+ };
410
420
  this.__crypto_randomUUID = {
411
421
  wasm: (scope, {allocPage,builtin,}) => [...number(allocPage(scope, 'bytestring: __crypto_randomUUID/bytes', 'i8') * pageSize, 127),[34,0],[34,1],[34,2],[65,16],[106],[33,3],[3,64],[32,2],[32,3],[72],[4,64],[32,2],[32,2],[65,1],[106],[33,2],[16, builtin('__Porffor_randomByte')],[58,0,4],[12,1],[11],[11],[32,1],[32,1],[45,0,10],[65,15],[113],[65,192,0],[114],[58,0,10],[32,1],[32,1],[45,0,12],[65,63],[113],[65,128,1],[114],[58,0,12],...number(allocPage(scope, 'bytestring: __crypto_randomUUID/output', 'i8') * pageSize, 127),[34,4],[33,5],[32,1],[33,6],[32,5],[65,8],[106],[33,7],[3,64],[32,5],[32,7],[72],[4,64],[32,6],[32,6],[65,1],[106],[33,6],[45,0,4],[34,8],[65,15],[113],[65,48],[106],[34,9],[65,57],[74],[4,64],[32,9],[65,39],[106],[33,9],[11],[32,8],[65,4],[117],[65,48],[106],[34,10],[65,57],[74],[4,64],[32,10],[65,39],[106],[33,10],[11],[32,5],[32,5],[65,1],[106],[33,5],[32,10],[58,0,4],[32,5],[32,5],[65,1],[106],[33,5],[32,9],[58,0,4],[12,1],[11],[11],[32,5],[65,1],[106],[34,5],[65,4],[106],[33,7],[3,64],[32,5],[32,7],[72],[4,64],[32,6],[32,6],[65,1],[106],[33,6],[45,0,4],[34,8],[65,15],[113],[65,48],[106],[34,9],[65,57],[74],[4,64],[32,9],[65,39],[106],[33,9],[11],[32,8],[65,4],[117],[65,48],[106],[34,10],[65,57],[74],[4,64],[32,10],[65,39],[106],[33,10],[11],[32,5],[32,5],[65,1],[106],[33,5],[32,10],[58,0,4],[32,5],[32,5],[65,1],[106],[33,5],[32,9],[58,0,4],[12,1],[11],[11],[32,5],[65,1],[106],[34,5],[65,4],[106],[33,7],[3,64],[32,5],[32,7],[72],[4,64],[32,6],[32,6],[65,1],[106],[33,6],[45,0,4],[34,8],[65,15],[113],[65,48],[106],[34,9],[65,57],[74],[4,64],[32,9],[65,39],[106],[33,9],[11],[32,8],[65,4],[117],[65,48],[106],[34,10],[65,57],[74],[4,64],[32,10],[65,39],[106],[33,10],[11],[32,5],[32,5],[65,1],[106],[33,5],[32,10],[58,0,4],[32,5],[32,5],[65,1],[106],[33,5],[32,9],[58,0,4],[12,1],[11],[11],[32,5],[65,1],[106],[34,5],[65,4],[106],[33,7],[3,64],[32,5],[32,7],[72],[4,64],[32,6],[32,6],[65,1],[106],[33,6],[45,0,4],[34,8],[65,15],[113],[65,48],[106],[34,9],[65,57],[74],[4,64],[32,9],[65,39],[106],[33,9],[11],[32,8],[65,4],[117],[65,48],[106],[34,10],[65,57],[74],[4,64],[32,10],[65,39],[106],[33,10],[11],[32,5],[32,5],[65,1],[106],[33,5],[32,10],[58,0,4],[32,5],[32,5],[65,1],[106],[33,5],[32,9],[58,0,4],[12,1],[11],[11],[32,5],[65,1],[106],[34,5],[65,12],[106],[33,7],[3,64],[32,5],[32,7],[72],[4,64],[32,6],[32,6],[65,1],[106],[33,6],[45,0,4],[34,8],[65,15],[113],[65,48],[106],[34,9],[65,57],[74],[4,64],[32,9],[65,39],[106],[33,9],[11],[32,8],[65,4],[117],[65,48],[106],[34,10],[65,57],[74],[4,64],[32,10],[65,39],[106],[33,10],[11],[32,5],[32,5],[65,1],[106],[33,5],[32,10],[58,0,4],[32,5],[32,5],[65,1],[106],[33,5],[32,9],[58,0,4],[12,1],[11],[11],[32,5],[65,1],[106],[33,5],[32,4],[15]],
412
422
  params: [],
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "porffor",
3
3
  "description": "a basic experimental wip aot optimizing js -> wasm engine/compiler/runtime in js",
4
- "version": "0.14.0-032e4ad08",
4
+ "version": "0.14.0-0ad2c8a7c",
5
5
  "author": "CanadaHonk",
6
6
  "license": "MIT",
7
7
  "scripts": {
package/porffor_tmp.c DELETED
@@ -1,202 +0,0 @@
1
- #include <stdint.h>
2
- #include <string.h>
3
- #include <stdio.h>
4
- #include <stdlib.h>
5
-
6
- typedef uint8_t i8;
7
- typedef uint16_t i16;
8
- typedef int32_t i32;
9
- typedef uint32_t u32;
10
- typedef int64_t i64;
11
- typedef uint64_t u64;
12
- typedef float f32;
13
- typedef double f64;
14
-
15
- f64 NAN = 0e+0/0e+0;
16
-
17
- struct ReturnValue {
18
- f64 value;
19
- i32 type;
20
- };
21
-
22
- char _memory[131072];
23
-
24
- int _argc; char** _argv;
25
- i32 i32_load(i32 align, i32 offset, i32 pointer) {
26
- i32 out;
27
- memcpy(&out, _memory + offset + pointer, sizeof(out));
28
- return out;
29
- }
30
-
31
- i32 i32_load8_u(i32 align, i32 offset, i32 pointer) {
32
- i8 out;
33
- memcpy(&out, _memory + offset + pointer, sizeof(out));
34
- return out;
35
- }
36
-
37
- void i32_store(i32 align, i32 offset, i32 pointer, i32 value) {
38
- memcpy(_memory + offset + pointer, &value, sizeof(value));
39
- }
40
-
41
- void __Porffor_readArgv(u32 index, u32 outPtr) {
42
- if (index >= _argc) {
43
- printf("expected %d arguments\n", index);
44
- exit(1);
45
- }
46
-
47
- char* arg = _argv[index];
48
-
49
- u32 read = 0;
50
- char* out = _memory + outPtr + 4;
51
- char ch;
52
- while ((ch = *(arg++)) != 0) {
53
- out[read++] = ch;
54
- }
55
-
56
- memcpy(_memory + outPtr, &read, sizeof(read));
57
- }
58
- void __Porffor_readFile(u32 pathPtr, u32 outPtr) {
59
- char* path = _memory + pathPtr + 4;
60
- FILE* fp = fopen(path, "r");
61
- if (fp == NULL) {
62
- printf("failed to open file: %s\n", path);
63
- exit(1);
64
- }
65
-
66
- u32 read = 0;
67
- char* out = _memory + outPtr + 4;
68
- char ch;
69
- while ((ch = fgetc(fp)) != EOF) {
70
- out[read++] = ch;
71
- }
72
-
73
- fclose(fp);
74
-
75
- memcpy(_memory + outPtr, &read, sizeof(read));
76
- }
77
-
78
- f64 out = 0;
79
- i32 outdtype = 0;
80
-
81
- void __Porffor_print(f64 x, i32 y) {
82
- i32 a = 0;
83
- i32 b = 0;
84
- i32 dtypeswitch_tmp = 0;
85
-
86
- dtypeswitch_tmp = y;
87
- // block
88
- // if
89
- if (dtypeswitch_tmp == 0) {
90
- printf("%g\n", x);
91
- goto j0;
92
- }
93
- // end
94
- j1:;
95
- // if
96
- if (dtypeswitch_tmp == 1) {
97
- // if
98
- if (((u32)x) != 0) {
99
- printf("%c", (int)(1.16e+2));
100
- printf("%c", (int)(1.14e+2));
101
- printf("%c", (int)(1.17e+2));
102
- printf("%c", (int)(1.01e+2));
103
- } else {
104
- printf("%c", (int)(1.02e+2));
105
- printf("%c", (int)(9.7e+1));
106
- printf("%c", (int)(1.08e+2));
107
- printf("%c", (int)(1.15e+2));
108
- printf("%c", (int)(1.01e+2));
109
- }
110
- // end
111
- j3:;
112
- goto j0;
113
- }
114
- // end
115
- j2:;
116
- // if
117
- if (dtypeswitch_tmp == 3) {
118
- printf("%c", (int)(1.17e+2));
119
- printf("%c", (int)(1.1e+2));
120
- printf("%c", (int)(1e+2));
121
- printf("%c", (int)(1.01e+2));
122
- printf("%c", (int)(1.02e+2));
123
- printf("%c", (int)(1.05e+2));
124
- printf("%c", (int)(1.1e+2));
125
- printf("%c", (int)(1.01e+2));
126
- printf("%c", (int)(1e+2));
127
- goto j0;
128
- }
129
- // end
130
- j4:;
131
- // if
132
- if (dtypeswitch_tmp == 4) {
133
- // if
134
- if (((u32)x) != 0) {
135
- printf("%c", (int)(1.23e+2));
136
- printf("%c", (int)(1.25e+2));
137
- } else {
138
- printf("%c", (int)(1.1e+2));
139
- printf("%c", (int)(1.17e+2));
140
- printf("%c", (int)(1.08e+2));
141
- printf("%c", (int)(1.08e+2));
142
- }
143
- // end
144
- j6:;
145
- goto j0;
146
- }
147
- // end
148
- j5:;
149
- // if
150
- if (dtypeswitch_tmp == 5) {
151
- printf("%c", (int)(1.02e+2));
152
- printf("%c", (int)(1.17e+2));
153
- printf("%c", (int)(1.1e+2));
154
- printf("%c", (int)(9.9e+1));
155
- printf("%c", (int)(1.16e+2));
156
- printf("%c", (int)(1.05e+2));
157
- printf("%c", (int)(1.11e+2));
158
- printf("%c", (int)(1.1e+2));
159
- printf("%c", (int)(3.2e+1));
160
- printf("%c", (int)(4e+1));
161
- printf("%c", (int)(4.1e+1));
162
- printf("%c", (int)(3.2e+1));
163
- printf("%c", (int)(1.23e+2));
164
- printf("%c", (int)(1.25e+2));
165
- goto j0;
166
- }
167
- // end
168
- j7:;
169
- // if
170
- if (dtypeswitch_tmp == 18) {
171
- a = (u32)x;
172
- b = i32_load(1, 0, a) + a;
173
- // loop
174
- j9:;
175
- printf("%c", (int)((f64)(i32_load8_u(0, 4, a))));
176
- a = a + 1;
177
- if (a != b) {
178
- goto j9;
179
- }
180
- // end
181
- goto j0;
182
- }
183
- // end
184
- j8:;
185
- printf("%g\n", x);
186
- // end
187
- j0:;
188
- }
189
-
190
- int main(int argc, char* argv[]) {
191
- _argc = argc; _argv = argv;
192
-
193
- out = 1e+0;
194
- outdtype = 18;
195
- i32_store(1, 0, 65536, 0);
196
- __Porffor_readArgv((u32)(0e+0), (u32)(6.5536e+4));
197
- __Porffor_readFile((u32)(undefined), (u32)(out));
198
- __Porffor_print(out, outdtype);
199
- printf("%c", (int)(1e+1));
200
-
201
- return 0;
202
- }