@junoscript/juno 0.1.0 → 0.1.2
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/dist/CLI/generate-juno-types.js +4 -1
- package/dist/CLI/junode.js +40 -14
- package/dist/Lexer/parser.d.ts +8 -0
- package/dist/Lexer/parser.js +58 -9
- package/dist/Modules/bytes.d.ts +15 -0
- package/dist/Modules/bytes.js +35 -0
- package/dist/Modules/index.d.ts +7 -1
- package/dist/Modules/index.js +5 -1
- package/dist/Modules/juno.d.ts +3 -0
- package/dist/Modules/juno.js +10 -2
- package/dist/Modules/types.d.ts +5 -0
- package/dist/Modules/types.js +10 -2
- package/dist/Runtime/interpreter.js +112 -15
- package/dist/vscode-junoscript/src/language.js +314 -39
- package/package.json +3 -2
|
@@ -265,7 +265,7 @@ class Interpreter {
|
|
|
265
265
|
throw new TypeError(`Constructor '${statement.name}' needs at least one anonymous function`);
|
|
266
266
|
this.registerTypeDeclarations(statement.inheritedType, env);
|
|
267
267
|
const overloads = statement.overloads.map(overload => ({ __junoFunction: true, params: overload.params, body: overload.body, closure: env, name: statement.name, file: this.currentFile, loc: statement.loc }));
|
|
268
|
-
const constructor = { __junoConstructor: true, typeName: statement.name, inheritedType: statement.inheritedType, overloads };
|
|
268
|
+
const constructor = { __junoConstructor: true, typeName: statement.name, inheritedType: statement.inheritedType, overloads, generics: statement.generics, genericDefaults: statement.genericDefaults };
|
|
269
269
|
for (const method of statement.methods)
|
|
270
270
|
constructor[method.name] = { __junoFunction: true, params: method.params, body: method.body, closure: env, name: `${statement.name}.${method.name}`, file: this.currentFile, loc: method.loc };
|
|
271
271
|
this.declare(env, statement.name, constructor, false);
|
|
@@ -832,7 +832,13 @@ class Interpreter {
|
|
|
832
832
|
return object[index] ?? null;
|
|
833
833
|
}
|
|
834
834
|
case 'Call': {
|
|
835
|
-
if (expression.callee.kind === 'Identifier' && expression.
|
|
835
|
+
if (expression.callee.kind === 'Identifier' && expression.typeArguments?.length &&
|
|
836
|
+
(expression.callee.name === 'Items' || expression.callee.name === 'Keys')) {
|
|
837
|
+
const name = expression.callee.name;
|
|
838
|
+
const itemTypes = expression.typeArguments;
|
|
839
|
+
if (name === 'Keys' && (itemTypes.length !== 1 || expression.args.length !== 0)) {
|
|
840
|
+
throw new TypeError('Keys<T>() expects one type argument and no value arguments');
|
|
841
|
+
}
|
|
836
842
|
const values = [];
|
|
837
843
|
const collect = (type) => {
|
|
838
844
|
if (type.kind === 'UnionType')
|
|
@@ -841,9 +847,12 @@ class Interpreter {
|
|
|
841
847
|
values.push(type.value);
|
|
842
848
|
return;
|
|
843
849
|
}
|
|
844
|
-
throw new TypeError(
|
|
850
|
+
throw new TypeError(`${name}<> requires finite string or number literal types; received '${typeName(type)}'`);
|
|
845
851
|
};
|
|
846
|
-
|
|
852
|
+
for (const type of itemTypes) {
|
|
853
|
+
const itemType = name === 'Keys' ? { kind: 'GenericType', name: 'KeyOf', args: [type] } : type;
|
|
854
|
+
collect(this.resolveAliasType(itemType, env));
|
|
855
|
+
}
|
|
847
856
|
return values;
|
|
848
857
|
}
|
|
849
858
|
if (expression.callee.kind === 'Member' && expression.callee.property === 'as' && expression.typeArguments?.length) {
|
|
@@ -915,10 +924,18 @@ class Interpreter {
|
|
|
915
924
|
});
|
|
916
925
|
if (!overload)
|
|
917
926
|
throw new TypeError(`No constructor overload accepts ${args.length} argument(s)`);
|
|
918
|
-
const
|
|
927
|
+
const closure = constructor.generics.length ? this.childEnvironment(overload.closure) : overload.closure;
|
|
928
|
+
constructor.generics.forEach((name, index) => {
|
|
929
|
+
const argument = expression.typeArguments?.[index];
|
|
930
|
+
const fallback = constructor.genericDefaults?.[index];
|
|
931
|
+
const value = argument ? this.resolveAliasType(argument, env) : fallback && this.resolveAliasType(fallback, closure);
|
|
932
|
+
if (value)
|
|
933
|
+
closure.types.set(name, { value, generics: [] });
|
|
934
|
+
});
|
|
935
|
+
const value = this.callFunction({ ...overload, closure }, args);
|
|
919
936
|
if (!value || typeof value !== 'object')
|
|
920
937
|
throw new TypeError('A constructor must return an object');
|
|
921
|
-
const result = this.applyTypeDefaults(value, constructor.inheritedType,
|
|
938
|
+
const result = this.applyTypeDefaults(value, constructor.inheritedType, closure);
|
|
922
939
|
return constructor.typeName === 'Tuple' ? Object.freeze(result) : result;
|
|
923
940
|
}
|
|
924
941
|
if (callee?.__junoTypeConstructor) {
|
|
@@ -1707,6 +1724,13 @@ class Interpreter {
|
|
|
1707
1724
|
results.push(object.kind === 'ArrayType' ? object.element : object.args[0] ?? { kind: 'NamedType', name: 'any' });
|
|
1708
1725
|
continue;
|
|
1709
1726
|
}
|
|
1727
|
+
if (part.kind === 'NamedType' && object.kind === 'ObjectType') {
|
|
1728
|
+
const member = object.members.find(item => !item.indexType && item.name === part.name);
|
|
1729
|
+
if (member) {
|
|
1730
|
+
results.push(member.type);
|
|
1731
|
+
continue;
|
|
1732
|
+
}
|
|
1733
|
+
}
|
|
1710
1734
|
if (part.kind !== 'LiteralType' || typeof part.value !== 'string' && typeof part.value !== 'number')
|
|
1711
1735
|
return null;
|
|
1712
1736
|
const member = this.findTypeMember(object, String(part.value), env);
|
|
@@ -1796,6 +1820,12 @@ class Interpreter {
|
|
|
1796
1820
|
return { kind: 'LiteralType', value };
|
|
1797
1821
|
if (typeof value === 'bigint')
|
|
1798
1822
|
return { kind: 'NamedType', name: 'BigInt' };
|
|
1823
|
+
if (value instanceof Date)
|
|
1824
|
+
return { kind: 'NamedType', name: 'Date' };
|
|
1825
|
+
if (Buffer.isBuffer(value))
|
|
1826
|
+
return { kind: 'NamedType', name: 'Buffer' };
|
|
1827
|
+
if (value instanceof Uint8Array)
|
|
1828
|
+
return { kind: 'NamedType', name: 'Uint8Array' };
|
|
1799
1829
|
if (typeof value === 'function' || value?.__junoFunction)
|
|
1800
1830
|
return { kind: 'NamedType', name: 'Function' };
|
|
1801
1831
|
if (Array.isArray(value)) {
|
|
@@ -1837,6 +1867,15 @@ class Interpreter {
|
|
|
1837
1867
|
if (type.kind === 'ArrayType')
|
|
1838
1868
|
return { ...type, element: this.resolveAliasType(type.element, env, resolving) };
|
|
1839
1869
|
if (type.kind === 'TypeOfType') {
|
|
1870
|
+
const indexed = (0, parser_1.indexedTypeQuery)(type.expression);
|
|
1871
|
+
if (indexed) {
|
|
1872
|
+
for (let current = env; current; current = current.parent) {
|
|
1873
|
+
if (current.types.has(indexed.object.name))
|
|
1874
|
+
return this.resolveAliasType(indexed, env, resolving);
|
|
1875
|
+
if (current.bindings.has(indexed.object.name))
|
|
1876
|
+
break;
|
|
1877
|
+
}
|
|
1878
|
+
}
|
|
1840
1879
|
const dependsOnUnresolvedType = type.expression.kind === 'Index'
|
|
1841
1880
|
&& type.expression.index.kind === 'Identifier'
|
|
1842
1881
|
&& resolving.has(type.expression.index.name);
|
|
@@ -1862,6 +1901,20 @@ class Interpreter {
|
|
|
1862
1901
|
return source.name === destination.name;
|
|
1863
1902
|
if (source.kind === 'ArrayType' && destination.kind === 'ArrayType')
|
|
1864
1903
|
return assignable(source.element, destination.element);
|
|
1904
|
+
if (source.kind === 'ObjectType' && destination.kind === 'ObjectType') {
|
|
1905
|
+
return destination.members.every(expected => {
|
|
1906
|
+
const actual = source.members.find(member => member.name === expected.name);
|
|
1907
|
+
return actual ? (!actual.optional || expected.optional) && assignable(actual.type, expected.type) : expected.optional;
|
|
1908
|
+
});
|
|
1909
|
+
}
|
|
1910
|
+
if (destination.kind === 'GenericType' && destination.name === 'Record' && destination.args.length === 2) {
|
|
1911
|
+
if (source.kind === 'GenericType' && source.name === 'Record' && source.args.length === 2) {
|
|
1912
|
+
return assignable(destination.args[0], source.args[0]) && assignable(source.args[1], destination.args[1]);
|
|
1913
|
+
}
|
|
1914
|
+
if (source.kind === 'ObjectType')
|
|
1915
|
+
return source.members.every(member => assignable(member.indexType ?? { kind: 'LiteralType', value: member.name }, destination.args[0]) &&
|
|
1916
|
+
assignable(member.type, destination.args[1]));
|
|
1917
|
+
}
|
|
1865
1918
|
return false;
|
|
1866
1919
|
};
|
|
1867
1920
|
return this.resolveAliasType(assignable(check, target) ? type.consequent : type.alternate, env, resolving);
|
|
@@ -1869,12 +1922,24 @@ class Interpreter {
|
|
|
1869
1922
|
if (type.kind === 'IndexedAccessType')
|
|
1870
1923
|
return this.indexedType(type.object, type.index, env) ?? { kind: 'NamedType', name: 'any' };
|
|
1871
1924
|
if (type.kind === 'ObjectType')
|
|
1872
|
-
return { ...type, members: type.members.
|
|
1873
|
-
|
|
1874
|
-
|
|
1875
|
-
indexType
|
|
1876
|
-
|
|
1877
|
-
|
|
1925
|
+
return { ...type, members: type.members.flatMap(member => {
|
|
1926
|
+
const indexType = member.indexType && this.resolveAliasType(member.indexType, env, resolving);
|
|
1927
|
+
const keys = indexType?.kind === 'UnionType' ? indexType.types : indexType ? [indexType] : [];
|
|
1928
|
+
if (indexType && keys.every(key => key.kind === 'LiteralType' && (typeof key.value === 'string' || typeof key.value === 'number'))) {
|
|
1929
|
+
return keys.map(key => {
|
|
1930
|
+
const local = this.childEnvironment(env);
|
|
1931
|
+
local.types.set(member.name, { value: key, generics: [] });
|
|
1932
|
+
return { ...member, name: String(key.value), indexType: undefined,
|
|
1933
|
+
type: this.resolveAliasType(member.type, local, resolving) };
|
|
1934
|
+
});
|
|
1935
|
+
}
|
|
1936
|
+
return [{
|
|
1937
|
+
...member,
|
|
1938
|
+
type: this.resolveAliasType(member.type, env, new Set([...resolving, ...(member.generics ?? []), ...(member.indexType ? [member.name] : [])])),
|
|
1939
|
+
indexType,
|
|
1940
|
+
params: member.params?.map(param => ({ ...param, type: param.type ? this.resolveAliasType(param.type, env, new Set([...resolving, ...(member.generics ?? [])])) : undefined }))
|
|
1941
|
+
}];
|
|
1942
|
+
}) };
|
|
1878
1943
|
if (type.kind === 'FunctionType')
|
|
1879
1944
|
return { ...type, params: type.params.map(param => this.resolveAliasType(param, env, resolving)), result: this.resolveAliasType(type.result, env, resolving) };
|
|
1880
1945
|
return type;
|
|
@@ -2001,6 +2066,9 @@ class Interpreter {
|
|
|
2001
2066
|
case 'boolean': return typeof value === 'boolean';
|
|
2002
2067
|
case 'BigInt': return typeof value === 'bigint';
|
|
2003
2068
|
case 'RegEx': return value instanceof RegExp;
|
|
2069
|
+
case 'Date': return value instanceof Date;
|
|
2070
|
+
case 'Uint8Array': return value instanceof Uint8Array;
|
|
2071
|
+
case 'Buffer': return Buffer.isBuffer(value);
|
|
2004
2072
|
case 'object': return value !== null && typeof value === 'object';
|
|
2005
2073
|
case 'Function': return typeof value === 'function' || Boolean(value?.__junoFunction);
|
|
2006
2074
|
default: return false;
|
|
@@ -2014,6 +2082,16 @@ class Interpreter {
|
|
|
2014
2082
|
if ((type.name === 'Array' || type.name === 'ReadonlyArray') && type.args[0]) {
|
|
2015
2083
|
return Array.isArray(value) && value.every(item => this.matchesTypeNode(item, type.args[0], env, resolving));
|
|
2016
2084
|
}
|
|
2085
|
+
if (type.name === 'Record' && type.args.length === 2) {
|
|
2086
|
+
if (value === null || typeof value !== 'object' || Array.isArray(value))
|
|
2087
|
+
return false;
|
|
2088
|
+
return Object.entries(value).every(([key, item]) => {
|
|
2089
|
+
const numeric = Number(key);
|
|
2090
|
+
const validKey = this.matchesTypeNode(key, type.args[0], env, resolving) ||
|
|
2091
|
+
(String(numeric) === key && this.matchesTypeNode(numeric, type.args[0], env, resolving));
|
|
2092
|
+
return validKey && this.matchesTypeNode(item, type.args[1], env, resolving);
|
|
2093
|
+
});
|
|
2094
|
+
}
|
|
2017
2095
|
const resolved = this.resolveAliasType(type, env);
|
|
2018
2096
|
if (resolved !== type)
|
|
2019
2097
|
return this.matchesTypeNode(value, resolved, env, resolving);
|
|
@@ -2205,6 +2283,17 @@ class Interpreter {
|
|
|
2205
2283
|
if (['at', 'charAt', 'concat', 'normalize', 'padEnd', 'padStart', 'repeat', 'replace', 'replaceAll', 'slice', 'substring', 'substr', 'toLocaleLowerCase', 'toLocaleUpperCase', 'toLowerCase', 'toUpperCase', 'trim', 'trimEnd', 'trimStart', 'valueOf'].includes(name))
|
|
2206
2284
|
return callable([], string);
|
|
2207
2285
|
}
|
|
2286
|
+
if (type.name === 'Date' && name !== 'constructor' && Object.hasOwn(Date.prototype, name)) {
|
|
2287
|
+
return { type: { kind: 'NamedType', name: 'Function' }, optional: false };
|
|
2288
|
+
}
|
|
2289
|
+
if (type.name === 'Uint8Array' || type.name === 'Buffer') {
|
|
2290
|
+
if (['length', 'byteLength', 'byteOffset'].includes(name))
|
|
2291
|
+
return { type: { kind: 'NamedType', name: 'number' }, optional: false };
|
|
2292
|
+
if (name === 'buffer')
|
|
2293
|
+
return { type: { kind: 'NamedType', name: 'ArrayBuffer' }, optional: false };
|
|
2294
|
+
if (['at', 'slice', 'subarray', 'set', 'toString'].includes(name))
|
|
2295
|
+
return { type: { kind: 'NamedType', name: 'Function' }, optional: false };
|
|
2296
|
+
}
|
|
2208
2297
|
if (type.name === 'RegEx') {
|
|
2209
2298
|
const string = { kind: 'NamedType', name: 'string' };
|
|
2210
2299
|
const boolean = { kind: 'NamedType', name: 'boolean' };
|
|
@@ -2263,9 +2352,14 @@ class Interpreter {
|
|
|
2263
2352
|
?? type.members.find(item => !item.indexType && item.name === name);
|
|
2264
2353
|
if (member)
|
|
2265
2354
|
return { type: member.type, optional: member.optional };
|
|
2266
|
-
const
|
|
2267
|
-
|
|
2268
|
-
|
|
2355
|
+
const numeric = Number(name);
|
|
2356
|
+
const key = Number.isFinite(numeric) && String(numeric) === name ? numeric : name;
|
|
2357
|
+
const indexMember = type.members.find(item => item.indexType && this.matchesTypeNode(key, item.indexType, env));
|
|
2358
|
+
if (indexMember) {
|
|
2359
|
+
const local = this.childEnvironment(env);
|
|
2360
|
+
local.types.set(indexMember.name, { value: { kind: 'LiteralType', value: key }, generics: [] });
|
|
2361
|
+
return { type: this.resolveAliasType(indexMember.type, local, resolving), optional: indexMember.optional };
|
|
2362
|
+
}
|
|
2269
2363
|
const call = type.members.find(item => item.name === '' && item.method);
|
|
2270
2364
|
if (call) {
|
|
2271
2365
|
const any = { kind: 'NamedType', name: 'any' };
|
|
@@ -2485,6 +2579,9 @@ function matchesType(value, type) {
|
|
|
2485
2579
|
case 'number': return typeof value === 'number';
|
|
2486
2580
|
case 'boolean': return typeof value === 'boolean';
|
|
2487
2581
|
case 'RegEx': return value instanceof RegExp;
|
|
2582
|
+
case 'Date': return value instanceof Date;
|
|
2583
|
+
case 'Uint8Array': return value instanceof Uint8Array;
|
|
2584
|
+
case 'Buffer': return Buffer.isBuffer(value);
|
|
2488
2585
|
case 'null': return value === null;
|
|
2489
2586
|
case 'Array': return Array.isArray(value);
|
|
2490
2587
|
case 'object': return value !== null && typeof value === 'object';
|