porffor 0.39.1 โ†’ 0.39.3

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.
@@ -6,7 +6,7 @@ export const __Porffor_object_underlying = (obj: any): any => {
6
6
 
7
7
  if (Porffor.fastAnd(
8
8
  t >= Porffor.TYPES.error,
9
- t <= Porffor.TYPES.urierror
9
+ t <= Porffor.TYPES.__porffor_todoerror
10
10
  )) {
11
11
  const remap: object = obj;
12
12
  return remap;
@@ -557,9 +557,44 @@ export const __Array_prototype_reduceRight = (_this: any[], callbackFn: any, ini
557
557
  return acc;
558
558
  };
559
559
 
560
+ // string less than <
561
+ export const __Porffor_strlt = (a: string|bytestring, b: string|bytestring) => {
562
+ const maxLength: i32 = Math.max(a.length, b.length);
563
+ for (let i: i32 = 0; i < maxLength; i++) {
564
+ const ac: i32 = a.charCodeAt(i);
565
+ const bc: i32 = b.charCodeAt(i);
566
+
567
+ if (ac < bc) return true;
568
+ }
569
+
570
+ return false;
571
+ };
572
+
560
573
  // @porf-typed-array
561
574
  export const __Array_prototype_sort = (_this: any[], callbackFn: any) => {
562
- // todo: default callbackFn
575
+ if (callbackFn === undefined) {
576
+ // default callbackFn, convert to strings and sort by char code
577
+ callbackFn = (x: any, y: any) => {
578
+ // 23.1.3.30.2 CompareArrayElements (x, y, comparefn)
579
+ // https://tc39.es/ecma262/#sec-comparearrayelements
580
+ // 5. Let xString be ? ToString(x).
581
+ const xString: any = ecma262.ToString(x);
582
+
583
+ // 6. Let yString be ? ToString(y).
584
+ const yString: any = ecma262.ToString(y);
585
+
586
+ // 7. Let xSmaller be ! IsLessThan(xString, yString, true).
587
+ // 8. If xSmaller is true, return -1๐”ฝ.
588
+ if (__Porffor_strlt(xString, yString)) return -1;
589
+
590
+ // 9. Let ySmaller be ! IsLessThan(yString, xString, true).
591
+ // 10. If ySmaller is true, return 1๐”ฝ.
592
+ if (__Porffor_strlt(yString, xString)) return 1;
593
+
594
+ // 11. Return +0๐”ฝ.
595
+ return 0;
596
+ };
597
+ }
563
598
 
564
599
  // insertion sort, i guess
565
600
  const len: i32 = _this.length;
@@ -584,8 +619,7 @@ export const __Array_prototype_sort = (_this: any[], callbackFn: any) => {
584
619
  else {
585
620
  // 4. If comparefn is not undefined, then
586
621
  // a. Let v be ? ToNumber(? Call(comparefn, undefined, ยซ x, y ยป)).
587
- // perf: unneeded as we just check >= 0
588
- // v = Number(callbackFn(x, y));
622
+ // perf: ToNumber unneeded as we just check >= 0
589
623
  v = callbackFn(x, y);
590
624
 
591
625
  // b. If v is NaN, return +0๐”ฝ.
@@ -41,8 +41,10 @@ export const __${name.startsWith('__') ? name.slice(2) : name}_prototype_toStrin
41
41
  error('EvalError');
42
42
  error('URIError');
43
43
 
44
- error('__Porffor_TodoError');
45
44
  error('Test262Error');
45
+ error('__Porffor_TodoError');
46
+
47
+ out += `\nexport const __Test262Error_thrower = message => { throw new Test262Error(message); };`;
46
48
 
47
49
  return out;
48
50
  };
@@ -94,7 +94,7 @@ export const __ecma262_RejectPromise = (promise: any[], reason: any): void => {
94
94
 
95
95
  export const __Porffor_promise_noop = () => {};
96
96
 
97
- export const __Porffor_promise_resolve = (promise: any, value: any): any => {
97
+ export const __Porffor_promise_resolve = (value: any, promise: any): any => {
98
98
  // todo: if value is own promise, reject with typeerror
99
99
 
100
100
  if (__ecma262_IsPromise(value)) {
@@ -106,7 +106,7 @@ export const __Porffor_promise_resolve = (promise: any, value: any): any => {
106
106
  return undefined;
107
107
  };
108
108
 
109
- export const __Porffor_promise_reject = (promise: any, reason: any): any => {
109
+ export const __Porffor_promise_reject = (reason: any, promise: any): any => {
110
110
  if (__ecma262_IsPromise(reason)) {
111
111
  // todo
112
112
  } else {
@@ -181,8 +181,8 @@ export const __Porffor_promise_runJobs = () => {
181
181
 
182
182
  // hack: cannot share scope so use a global
183
183
  let activePromise: any;
184
- export const __Porffor_promise_resolveActive = (value: any) => __Porffor_promise_resolve(activePromise, value);
185
- export const __Porffor_promise_rejectActive = (reason: any) => __Porffor_promise_reject(activePromise, reason);
184
+ export const __Porffor_promise_resolveActive = (value: any) => __Porffor_promise_resolve(value, activePromise);
185
+ export const __Porffor_promise_rejectActive = (reason: any) => __Porffor_promise_reject(reason, activePromise);
186
186
 
187
187
  export const Promise = function (executor: any): void {
188
188
  if (!new.target) throw new TypeError("Constructor Promise requires 'new'");
@@ -191,7 +191,13 @@ export const Promise = function (executor: any): void {
191
191
  const obj: any[] = __Porffor_promise_create();
192
192
 
193
193
  activePromise = obj;
194
- executor(__Porffor_promise_resolveActive, __Porffor_promise_rejectActive);
194
+
195
+ try {
196
+ executor(__Porffor_promise_resolveActive, __Porffor_promise_rejectActive);
197
+ } catch (e) {
198
+ // executor threw, reject promise
199
+ __ecma262_RejectPromise(obj, e);
200
+ }
195
201
 
196
202
  const pro: Promise = obj;
197
203
  return pro;
@@ -200,7 +206,7 @@ export const Promise = function (executor: any): void {
200
206
  export const __Promise_resolve = (value: any): Promise => {
201
207
  const obj: any[] = __Porffor_promise_create();
202
208
 
203
- __Porffor_promise_resolve(obj, value);
209
+ __Porffor_promise_resolve(value, obj);
204
210
 
205
211
  const pro: Promise = obj;
206
212
  return pro;
@@ -209,7 +215,7 @@ export const __Promise_resolve = (value: any): Promise => {
209
215
  export const __Promise_reject = (reason: any): Promise => {
210
216
  const obj: any[] = __Porffor_promise_create();
211
217
 
212
- __Porffor_promise_reject(obj, reason);
218
+ __Porffor_promise_reject(reason, obj);
213
219
 
214
220
  const pro: Promise = obj;
215
221
  return pro;
@@ -431,6 +437,5 @@ export const __Porffor_promise_await = (value: any) => {
431
437
  if (state == 1) return result;
432
438
 
433
439
  // rejected
434
- // todo: throw result instead of fixed error here
435
- throw Error('Uncaught await promise rejection');
440
+ throw result;
436
441
  };
@@ -88,6 +88,7 @@ export const __Reflect_ownKeys = (target: any) => {
88
88
 
89
89
  const out: any[] = Porffor.allocate();
90
90
 
91
+ target = __Porffor_object_underlying(target);
91
92
  const t: i32 = Porffor.rawType(target);
92
93
  if (t == Porffor.TYPES.object) {
93
94
  let ptr: i32 = Porffor.wasm`local.get ${target}` + 5;
@@ -132,25 +133,6 @@ local.set ${key}`;
132
133
  }
133
134
 
134
135
  out.length = i;
135
- } else {
136
- if (Porffor.fastOr(
137
- t == Porffor.TYPES.array,
138
- t == Porffor.TYPES.bytestring,
139
- t == Porffor.TYPES.string
140
- )) {
141
- const len: i32 = obj.length;
142
- out.length = len;
143
-
144
- for (let i: i32 = 0; i < len; i++) {
145
- out[i] = __Number_prototype_toString(i);
146
- }
147
- }
148
-
149
- target = __Porffor_object_underlying(target);
150
- if (Porffor.rawType(target) == Porffor.TYPES.object) {
151
- const objKeys: any[] = __Reflect_ownKeys(target);
152
- for (const x of objKeys) Porffor.array.fastPush(out, x);
153
- }
154
136
  }
155
137
 
156
138
  return out;
@@ -1609,4 +1609,4 @@ export const __String_prototype_valueOf = (_this: string) => {
1609
1609
  export const __ByteString_prototype_valueOf = (_this: bytestring) => {
1610
1610
  // 1. Return ? ThisStringValue(this value).
1611
1611
  return _this;
1612
- };
1612
+ };