Skip to content

Commit

Permalink
Merge pull request #1188 from bbrk24/more-comptime-types
Browse files Browse the repository at this point in the history
Serialize typed arrays and well-known symbols
  • Loading branch information
edemaine authored Apr 27, 2024
2 parents 0e7810d + c4df4c0 commit f929c6d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
14 changes: 14 additions & 0 deletions source/parser/comptime.civet
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,12 @@ function serialize(value: ???): string
<? "symbol"
if key? := Symbol.keyFor val
return `Symbol.for(${JSON.stringify key})`
// Handle well-known symbols
// for-in doesn't find them, but getOwnPropertyNames does
for name of Object.getOwnPropertyNames Symbol
sym := (Symbol as! Record<string, symbol>)[name]
if val is sym
return `Symbol.${name}`
throw new TypeError "cannot serialize unique symbol"
<? "object"
if stack.has val
Expand Down Expand Up @@ -116,6 +122,14 @@ function serialize(value: ???): string
for own key, value in val as {[key: string]: ???}
`${JSON.stringify key}:${recurse value}`
).join(",") + "}"
when Int8Array::, Uint8Array::, Int16Array::, Uint16Array::, Int32Array::, Uint32Array::, Float32Array::, Float64Array::, Uint8ClampedArray::
// There's no "TypedArray" interface in TS
`new ${val.constructor.name}([${(val as any).join ','}])`
when BigInt64Array::, BigUint64Array::
`new ${val.constructor.name}([${Array.from(val as ArrayLike<bigint>, `${&}n`).join ','}])`
// Spelled differently for browsers, where `Buffer` doesn't exist
when globalThis.Buffer?.prototype
`Buffer.from([${(val as Buffer).join ','}])`
else
// One day we may handle other classes like so:
// str += `__proto__:${val.constructor.name}`
Expand Down
7 changes: 7 additions & 0 deletions test/comptime.civet
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ describe "serialize", ->
assert.equal serialize(Symbol.for 'MyKeyedSymbol'),
'Symbol.for("MyKeyedSymbol")'
assert.throws (=> serialize Symbol()), /cannot serialize unique symbol/
assert.equal serialize(Symbol.iterator), "Symbol.iterator"
it "regexps", =>
assert.equal serialize(/a[b\s]c/), "/a[b\\s]c/"
assert.equal serialize(/abc/gi), "/abc/gi"
Expand Down Expand Up @@ -164,3 +165,9 @@ describe "serialize", ->
assert.equal serialize({ functionF() { } }.functionF), "function functionF() { }"
assert.equal serialize({ async bar() { } }.bar), "async function bar() { }"
assert.throws (=> serialize { [Math.sqrt 5]() {} }), /cannot serialize method with computed name/
it "typed arrays", =>
assert.equal serialize(new Uint32Array [1, 2, 3]), "new Uint32Array([1,2,3])"
assert.equal serialize(new Float64Array [1.0, 1.5, NaN]), "new Float64Array([1,1.5,NaN])"
assert.equal serialize(new BigInt64Array [1n, 2n, 3n]), "new BigInt64Array([1n,2n,3n])"
assert.equal serialize(Buffer.from [1, 2, 3]), "Buffer.from([1,2,3])"
assert.equal serialize(new Uint8ClampedArray [-1, 0, 2, 256]), "new Uint8ClampedArray([0,0,2,255])"

0 comments on commit f929c6d

Please sign in to comment.