Compare commits

...

7 Commits

9 changed files with 357 additions and 18 deletions

View File

@ -0,0 +1,292 @@
import { describe, test, expect } from 'bun:test'
import { Compiler } from '#compiler/compiler'
import { VM } from 'reefvm'
describe('Native Function Exceptions', () => {
test('native function error caught by try/catch', async () => {
const code = `
result = try:
failing-fn
catch e:
'caught: ' + e
end
result
`
const compiler = new Compiler(code)
const vm = new VM(compiler.bytecode)
vm.set('failing-fn', () => {
throw new Error('native function failed')
})
const result = await vm.run()
expect(result).toEqual({ type: 'string', value: 'caught: native function failed' })
})
test('async native function error caught by try/catch', async () => {
const code = `
result = try:
async-fail
catch e:
'async caught: ' + e
end
result
`
const compiler = new Compiler(code)
const vm = new VM(compiler.bytecode)
vm.set('async-fail', async () => {
await new Promise(resolve => setTimeout(resolve, 1))
throw new Error('async error')
})
const result = await vm.run()
expect(result).toEqual({ type: 'string', value: 'async caught: async error' })
})
test('native function with arguments throwing error', async () => {
const code = `
result = try:
read-file missing.txt
catch e:
'default content'
end
result
`
const compiler = new Compiler(code)
const vm = new VM(compiler.bytecode)
vm.set('read-file', (path: string) => {
if (path === 'missing.txt') {
throw new Error('file not found')
}
return 'file contents'
})
const result = await vm.run()
expect(result).toEqual({ type: 'string', value: 'default content' })
})
test('native function error with finally block', async () => {
const code = `
cleanup-count = 0
result = try:
failing-fn
catch e:
'error handled'
finally:
cleanup-count = cleanup-count + 1
end
cleanup-count
`
const compiler = new Compiler(code)
const vm = new VM(compiler.bytecode)
vm.set('failing-fn', () => {
throw new Error('native error')
})
const result = await vm.run()
expect(result).toEqual({ type: 'number', value: 1 })
})
test('native function error without catch propagates', async () => {
const code = `
failing-fn
`
const compiler = new Compiler(code)
const vm = new VM(compiler.bytecode)
vm.set('failing-fn', () => {
throw new Error('uncaught error')
})
await expect(vm.run()).rejects.toThrow('uncaught error')
})
test('native function in function-level catch', async () => {
const code = `
safe-read = do path:
read-file path
catch e:
'default: ' + e
end
result = safe-read missing.txt
result
`
const compiler = new Compiler(code)
const vm = new VM(compiler.bytecode)
vm.set('read-file', (path: string) => {
throw new Error('file not found: ' + path)
})
const result = await vm.run()
expect(result).toEqual({ type: 'string', value: 'default: file not found: missing.txt' })
})
test('nested native function errors', async () => {
const code = `
result = try:
try:
inner-fail
catch e:
throw 'wrapped: ' + e
end
catch e:
'outer caught: ' + e
end
result
`
const compiler = new Compiler(code)
const vm = new VM(compiler.bytecode)
vm.set('inner-fail', () => {
throw new Error('inner error')
})
const result = await vm.run()
expect(result).toEqual({ type: 'string', value: 'outer caught: wrapped: inner error' })
})
test('native function error with multiple named args', async () => {
const code = `
result = try:
process-file path=missing.txt mode=strict
catch e:
'error: ' + e
end
result
`
const compiler = new Compiler(code)
const vm = new VM(compiler.bytecode)
vm.set('process-file', (path: string, mode: string = 'lenient') => {
if (mode === 'strict' && path === 'missing.txt') {
throw new Error('strict mode: file required')
}
return 'processed'
})
const result = await vm.run()
expect(result).toEqual({ type: 'string', value: 'error: strict mode: file required' })
})
test('native function returning normally after other functions threw', async () => {
const code = `
result1 = try:
failing-fn
catch e:
'caught'
end
result2 = success-fn
result1 + ' then ' + result2
`
const compiler = new Compiler(code)
const vm = new VM(compiler.bytecode)
vm.set('failing-fn', () => {
throw new Error('error')
})
vm.set('success-fn', () => {
return 'success'
})
const result = await vm.run()
expect(result).toEqual({ type: 'string', value: 'caught then success' })
})
test('native function error message preserved', async () => {
const code = `
result = try:
throw-custom-message
catch e:
e
end
result
`
const compiler = new Compiler(code)
const vm = new VM(compiler.bytecode)
vm.set('throw-custom-message', () => {
throw new Error('This is a very specific error message with details')
})
const result = await vm.run()
expect(result).toEqual({
type: 'string',
value: 'This is a very specific error message with details'
})
})
test('native function throwing non-Error value', async () => {
const code = `
result = try:
throw-string
catch e:
'caught: ' + e
end
result
`
const compiler = new Compiler(code)
const vm = new VM(compiler.bytecode)
vm.set('throw-string', () => {
throw 'plain string error'
})
const result = await vm.run()
expect(result).toEqual({ type: 'string', value: 'caught: plain string error' })
})
test('multiple native function calls with mixed success/failure', async () => {
const code = `
r1 = try: success-fn catch e: 'error' end
r2 = try: failing-fn catch e: 'caught' end
r3 = try: success-fn catch e: 'error' end
results = [r1 r2 r3]
results
`
const compiler = new Compiler(code)
const vm = new VM(compiler.bytecode)
vm.set('success-fn', () => 'ok')
vm.set('failing-fn', () => {
throw new Error('failed')
})
const result = await vm.run()
expect(result.type).toBe('array')
const arr = result.value as any[]
expect(arr.length).toBe(3)
expect(arr[0]).toEqual({ type: 'string', value: 'ok' })
expect(arr[1]).toEqual({ type: 'string', value: 'caught' })
expect(arr[2]).toEqual({ type: 'string', value: 'ok' })
})
})

View File

@ -236,7 +236,12 @@ export const getStringParts = (node: SyntaxNode, input: string) => {
}
})
return { parts, hasInterpolation: parts.length > 0 }
// hasInterpolation means the string has interpolation ($var) or escape sequences (\n)
// A simple string like 'hello' has one StringFragment but no interpolation
const hasInterpolation = parts.some(
(p) => p.type.id === terms.Interpolation || p.type.id === terms.EscapeSeq
)
return { parts, hasInterpolation }
}
export const getDotGetParts = (node: SyntaxNode, input: string) => {

View File

@ -159,7 +159,7 @@ TryBlock {
}
Throw {
@specialize[@name=keyword]<Identifier, "throw"> expression
@specialize[@name=keyword]<Identifier, "throw"> (BinOp | ConditionalOp | expression)
}
ConditionalOp {

View File

@ -7,9 +7,9 @@ import {highlighting} from "./highlight"
const spec_Identifier = {__proto__:null,catch:82, finally:88, end:90, null:96, try:106, throw:110, if:114, elseif:122, else:126}
export const parser = LRParser.deserialize({
version: 14,
states: "8lQYQbOOO#tQcO'#CvO$qOSO'#CxO%PQbO'#E`OOQ`'#DR'#DROOQa'#DO'#DOO&SQbO'#D]O'XQcO'#ETOOQa'#ET'#ETO)cQcO'#ESO*bQRO'#CwO+WQcO'#EOO+hQcO'#EOO+rQbO'#CuO,jOpO'#CsOOQ`'#EP'#EPO,oQbO'#EOO,vQQO'#EfOOQ`'#Db'#DbO,{QbO'#DdO,{QbO'#EhOOQ`'#Df'#DfO-pQRO'#DnOOQ`'#EO'#EOO-uQQO'#D}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PVq#U#VQ!PUQ!acQ!ddQ!ilQ!t|W!x!P!t!y#bQ!z!QQ!{!RQ!|!SQ#O!TQ#P!UQ#R!VR$o$bpXOk!`!f!p#Z#[#_#m#r#z$[$]$k$y$zzmPUVcdlq|!P!Q!R!S!T!U!V!t!y#U#V#b$bR!W]TuQv!PSOPV]klq!`!f!p#U#V#Z#[#_#m#r#z$[$]$k$y$zU#n#Z$]$kQ#v#_V$T#r$y$zZpPVq#U#VqbOk!`!f!p#Z#[#_#m#r#z$[$]$k$y$zqeOk!`!f!p#Z#[#_#m#r#z$[$]$k$y$z",
nodeNames: "⚠ Star Slash Plus Minus And Or Eq EqEq Neq Lt Lte Gt Gte Modulo Identifier AssignableIdentifier Word IdentifierBeforeDot Do Program PipeExpr FunctionCall DotGet Number ParenExpr FunctionCallOrIdentifier BinOp String StringFragment Interpolation EscapeSeq Boolean Regex Dict NamedArg NamedArgPrefix FunctionDef Params colon CatchExpr keyword TryBlock FinallyExpr keyword keyword Underscore Array Null ConditionalOp PositionalArg operator TryExpr keyword Throw keyword IfExpr keyword SingleLineThenBlock ThenBlock ElseIfExpr keyword ElseExpr keyword Assign",
maxTerm: 106,
context: trackScope,
@ -23,5 +23,5 @@ export const parser = LRParser.deserialize({
tokenizers: [operatorTokenizer, 1, 2, 3, tokenizer, new LocalTokenGroup("[~RP!O!PU~ZO!t~~", 11)],
topRules: {"Program":[0,20]},
specialized: [{term: 15, get: (value: any, stack: any) => (specializeKeyword(value, stack) << 1), external: specializeKeyword},{term: 15, get: (value: keyof typeof spec_Identifier) => spec_Identifier[value] || -1}],
tokenPrec: 1463
tokenPrec: 1547
})

View File

@ -39,7 +39,7 @@ export const globals = {
switch (value.type) {
case 'string': case 'array': return value.value.length
case 'dict': return value.value.size
default: return 0
default: throw new Error(`length: expected string, array, or dict, got ${value.type}`)
}
},
@ -65,7 +65,24 @@ export const globals = {
identity: (v: any) => v,
// collections
at: (collection: any, index: number | string) => collection[index],
at: (collection: any, index: number | string) => {
const value = toValue(collection)
if (value.type === 'string' || value.type === 'array') {
const idx = typeof index === 'number' ? index : parseInt(index as string)
if (idx < 0 || idx >= value.value.length) {
throw new Error(`at: index ${idx} out of bounds for ${value.type} of length ${value.value.length}`)
}
return value.value[idx]
} else if (value.type === 'dict') {
const key = String(index)
if (!value.value.has(key)) {
throw new Error(`at: key '${key}' not found in dict`)
}
return value.value.get(key)
} else {
throw new Error(`at: expected string, array, or dict, got ${value.type}`)
}
},
range: (start: number, end: number | null) => {
if (end === null) {
end = start

View File

@ -86,8 +86,14 @@ export const list = {
first: (list: any[]) => list[0] ?? null,
last: (list: any[]) => list[list.length - 1] ?? null,
rest: (list: any[]) => list.slice(1),
take: (list: any[], n: number) => list.slice(0, n),
drop: (list: any[], n: number) => list.slice(n),
take: (list: any[], n: number) => {
if (n < 0) throw new Error(`take: count must be non-negative, got ${n}`)
return list.slice(0, n)
},
drop: (list: any[], n: number) => {
if (n < 0) throw new Error(`drop: count must be non-negative, got ${n}`)
return list.slice(n)
},
append: (list: any[], item: any) => [...list, item],
prepend: (list: any[], item: any) => [item, ...list],
'index-of': (list: any[], item: any) => list.indexOf(item),

View File

@ -3,12 +3,24 @@ export const math = {
floor: (n: number) => Math.floor(n),
ceil: (n: number) => Math.ceil(n),
round: (n: number) => Math.round(n),
min: (...nums: number[]) => Math.min(...nums),
max: (...nums: number[]) => Math.max(...nums),
min: (...nums: number[]) => {
if (nums.length === 0) throw new Error('min: expected at least one argument')
return Math.min(...nums)
},
max: (...nums: number[]) => {
if (nums.length === 0) throw new Error('max: expected at least one argument')
return Math.max(...nums)
},
pow: (base: number, exp: number) => Math.pow(base, exp),
sqrt: (n: number) => Math.sqrt(n),
sqrt: (n: number) => {
if (n < 0) throw new Error(`sqrt: cannot take square root of negative number ${n}`)
return Math.sqrt(n)
},
random: () => Math.random(),
clamp: (n: number, min: number, max: number) => Math.min(Math.max(n, min), max),
clamp: (n: number, min: number, max: number) => {
if (min > max) throw new Error(`clamp: min (${min}) must be less than or equal to max (${max})`)
return Math.min(Math.max(n, min), max)
},
sign: (n: number) => Math.sign(n),
trunc: (n: number) => Math.trunc(n),

View File

@ -21,7 +21,11 @@ export const str = {
'replace-all': (str: string, search: string, replacement: string) => str.replaceAll(search, replacement),
slice: (str: string, start: number, end?: number | null) => str.slice(start, end ?? undefined),
substring: (str: string, start: number, end?: number | null) => str.substring(start, end ?? undefined),
repeat: (str: string, count: number) => str.repeat(count),
repeat: (str: string, count: number) => {
if (count < 0) throw new Error(`repeat: count must be non-negative, got ${count}`)
if (!Number.isInteger(count)) throw new Error(`repeat: count must be an integer, got ${count}`)
return str.repeat(count)
},
'pad-start': (str: string, length: number, pad: string = ' ') => str.padStart(length, pad),
'pad-end': (str: string, length: number, pad: string = ' ') => str.padEnd(length, pad),
lines: (str: string) => str.split('\n'),

View File

@ -176,9 +176,12 @@ describe('introspection', () => {
await expect(`length 'hello'`).toEvaluateTo(5, globals)
await expect(`length [1 2 3]`).toEvaluateTo(3, globals)
await expect(`length [a=1 b=2]`).toEvaluateTo(2, globals)
await expect(`length 42`).toEvaluateTo(0, globals)
await expect(`length true`).toEvaluateTo(0, globals)
await expect(`length null`).toEvaluateTo(0, globals)
})
test('length throws on invalid types', async () => {
await expect(`try: length 42 catch e: 'error' end`).toEvaluateTo('error', globals)
await expect(`try: length true catch e: 'error' end`).toEvaluateTo('error', globals)
await expect(`try: length null catch e: 'error' end`).toEvaluateTo('error', globals)
})
test('inspect formats values', async () => {