porffor 0.28.3 → 0.28.5
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/compiler/codegen.js +44 -7
- package/package.json +1 -1
- package/runner/index.js +1 -1
package/compiler/codegen.js
CHANGED
@@ -2896,7 +2896,7 @@ const generateVar = (scope, decl) => {
|
|
2896
2896
|
const topLevel = scope.name === 'main';
|
2897
2897
|
|
2898
2898
|
// global variable if in top scope (main) or if internally wanted
|
2899
|
-
const global = topLevel || decl._bare;
|
2899
|
+
const global = decl._global ?? (topLevel || decl._bare);
|
2900
2900
|
|
2901
2901
|
for (const x of decl.declarations) {
|
2902
2902
|
if (x.id.type === 'ArrayPattern') {
|
@@ -2905,7 +2905,6 @@ const generateVar = (scope, decl) => {
|
|
2905
2905
|
|
2906
2906
|
let i = 0;
|
2907
2907
|
const elements = [...x.id.elements];
|
2908
|
-
// todo: if elements.length == 0 and Porffor.rawType(tmpName) is not iterable, throw a typeerror
|
2909
2908
|
for (const e of elements) {
|
2910
2909
|
switch (e?.type) {
|
2911
2910
|
case 'RestElement': { // let [ ...foo ] = []
|
@@ -2958,9 +2957,9 @@ const generateVar = (scope, decl) => {
|
|
2958
2957
|
break;
|
2959
2958
|
}
|
2960
2959
|
|
2961
|
-
case 'ArrayPattern': // let [ [ foo, bar ] ] = []
|
2960
|
+
case 'ArrayPattern': // let [ [ foo, bar ] ] = [ [ 2, 4 ] ]
|
2962
2961
|
case 'Identifier': // let [ foo ] = []
|
2963
|
-
case 'ObjectPattern': { // let [ { foo } ] = []
|
2962
|
+
case 'ObjectPattern': { // let [ { foo } ] = [ { foo: true } ]
|
2964
2963
|
decls.push({
|
2965
2964
|
type: 'VariableDeclarator',
|
2966
2965
|
id: e,
|
@@ -2987,8 +2986,27 @@ const generateVar = (scope, decl) => {
|
|
2987
2986
|
id: { type: 'Identifier', name: tmpName },
|
2988
2987
|
init: x.init
|
2989
2988
|
}],
|
2990
|
-
kind: decl.kind
|
2989
|
+
kind: decl.kind,
|
2990
|
+
_global: false
|
2991
2991
|
}),
|
2992
|
+
|
2993
|
+
// check tmp is iterable
|
2994
|
+
// array or string or bytestring
|
2995
|
+
...typeIsOneOf(getType(scope, tmpName), [ TYPES.array, TYPES.string, TYPES.bytestring ]),
|
2996
|
+
// typed array
|
2997
|
+
...getType(scope, tmpName),
|
2998
|
+
...number(TYPES.uint8array, Valtype.i32),
|
2999
|
+
[ Opcodes.i32_ge_s ],
|
3000
|
+
...getType(scope, tmpName),
|
3001
|
+
...number(TYPES.float64array, Valtype.i32),
|
3002
|
+
[ Opcodes.i32_le_s ],
|
3003
|
+
[ Opcodes.i32_and ],
|
3004
|
+
[ Opcodes.i32_or ],
|
3005
|
+
[ Opcodes.i32_eqz ],
|
3006
|
+
[ Opcodes.if, Blocktype.void ],
|
3007
|
+
...internalThrow(scope, 'TypeError', 'Cannot array destructure a non-iterable'),
|
3008
|
+
[ Opcodes.end ],
|
3009
|
+
|
2992
3010
|
...generateVar(scope, {
|
2993
3011
|
type: 'VariableDeclaration',
|
2994
3012
|
declarations: decls,
|
@@ -3004,7 +3022,6 @@ const generateVar = (scope, decl) => {
|
|
3004
3022
|
const tmpName = '#destructure' + uniqId();
|
3005
3023
|
|
3006
3024
|
const properties = [...x.id.properties];
|
3007
|
-
// todo: if properties.length == 0 and Porffor.rawType(tmpName) != object, throw a typeerror
|
3008
3025
|
for (const prop of properties) {
|
3009
3026
|
if (prop.type == 'Property') { // let { foo } = {}
|
3010
3027
|
if (prop.value.type === 'AssignmentPattern') { // let { foo = defaultValue } = {}
|
@@ -3048,8 +3065,28 @@ const generateVar = (scope, decl) => {
|
|
3048
3065
|
id: { type: 'Identifier', name: tmpName },
|
3049
3066
|
init: x.init
|
3050
3067
|
}],
|
3051
|
-
kind: decl.kind
|
3068
|
+
kind: decl.kind,
|
3069
|
+
_global: false
|
3052
3070
|
}),
|
3071
|
+
|
3072
|
+
// check tmp is valid object
|
3073
|
+
// not undefined or empty type
|
3074
|
+
...typeIsOneOf(getType(scope, tmpName), [ TYPES.undefined, TYPES.empty ]),
|
3075
|
+
|
3076
|
+
// not null
|
3077
|
+
...getType(scope, tmpName),
|
3078
|
+
...number(TYPES.object, Valtype.i32),
|
3079
|
+
[ Opcodes.i32_eq ],
|
3080
|
+
[ Opcodes.local_get, scope.locals[tmpName].idx ],
|
3081
|
+
...number(0),
|
3082
|
+
[ Opcodes.eq ],
|
3083
|
+
[ Opcodes.i32_and ],
|
3084
|
+
|
3085
|
+
[ Opcodes.i32_or ],
|
3086
|
+
[ Opcodes.if, Blocktype.void ],
|
3087
|
+
...internalThrow(scope, 'TypeError', 'Cannot object destructure undefined or null'),
|
3088
|
+
[ Opcodes.end ],
|
3089
|
+
|
3053
3090
|
...generateVar(scope, {
|
3054
3091
|
type: 'VariableDeclaration',
|
3055
3092
|
declarations: decls,
|
package/package.json
CHANGED