@@ -25,9 +25,11 @@ import type {
2525} from './JSON.type' ;
2626import type { TreeInterpreter } from './TreeInterpreter' ;
2727
28- import stringify from 'fast-json-stable-stringify' ;
29- import hash from 'hash.js' ;
28+ import { sha256 } from '@noble/hashes/sha256' ;
29+ import { sha512 } from '@noble/hashes/sha512' ;
30+ import { bytesToHex } from '@noble/hashes/utils' ;
3031import { NIL , v4 as uuidv4 , v5 as uuidv5 } from 'uuid' ;
32+ import jsonStringify from './utils/json-serialize' ;
3133
3234export enum InputArgument {
3335 TYPE_NUMBER = 0 ,
@@ -631,28 +633,41 @@ export class Runtime {
631633 return condition ? thenValue : elseValue ?? null ;
632634 } ;
633635
634- private functionRange : RuntimeFunction < [ number , number , string ] , Array < number | string > > = ( [ start , end , prefix ] ) => {
635- return Array . from ( { length : end - start } , ( _ , i ) => ( prefix ? `${ prefix } ${ i + start } ` : i + start ) ) ;
636+ private functionRange : RuntimeFunction < [ number , number ?, string ?] , Array < number | string > > = ( [
637+ start ,
638+ end ,
639+ prefix ,
640+ ] ) => {
641+ if ( end === undefined ) {
642+ end = start ;
643+ start = 0 ;
644+ }
645+
646+ return Array . from ( { length : end - start } , ( _ , i ) => ( prefix !== undefined ? `${ prefix } ${ i + start } ` : i + start ) ) ;
636647 } ;
637648
638649 private functionToObject : RuntimeFunction < [ JSONArrayKeyValuePairs ] , JSONObject > = ( [ array ] ) => {
639650 return Object . fromEntries ( array ) ;
640651 } ;
641652
642653 private functionJsonSerialize : RuntimeFunction < [ JSONValue ] , string > = ( [ inputValue ] ) => {
643- return stringify ( inputValue ) ;
654+ const result = jsonStringify ( inputValue ) ;
655+ if ( result === undefined ) {
656+ throw new Error ( 'invalid-value' ) ;
657+ }
658+ return result ;
644659 } ;
645660
646661 private functionJsonParse : RuntimeFunction < [ string ] , JSONValue > = ( [ inputValue ] ) => {
647662 return JSON . parse ( inputValue ) ;
648663 } ;
649664
650665 private functionSha256 : RuntimeFunction < [ string ] , string > = ( [ inputValue ] ) => {
651- return hash . sha256 ( ) . update ( inputValue ) . digest ( 'hex' ) ;
666+ return bytesToHex ( sha256 ( inputValue ) ) ;
652667 } ;
653668
654669 private functionSha512 : RuntimeFunction < [ string ] , string > = ( [ inputValue ] ) => {
655- return hash . sha512 ( ) . update ( inputValue ) . digest ( 'hex' ) ;
670+ return bytesToHex ( sha512 ( inputValue ) ) ;
656671 } ;
657672
658673 private functionUuid : RuntimeFunction < [ string ?, string ?] , string > = ( [ name , ns ] ) => {
@@ -663,7 +678,7 @@ export class Runtime {
663678 // Match the pattern between slashes and any flags after the last slash
664679 const match = regexString . match ( / ^ \/ ( .* ?) \/ ( [ g i m s u y ] * ) $ / ) ;
665680 if ( ! match ) {
666- throw new Error ( 'Invalid regex string format ' ) ;
681+ throw new Error ( 'invalid- regex' ) ;
667682 }
668683 return new RegExp ( match [ 1 ] , match [ 2 ] ) ;
669684 }
@@ -1124,6 +1139,7 @@ export class Runtime {
11241139 } ,
11251140 {
11261141 types : [ InputArgument . TYPE_NUMBER ] ,
1142+ optional : true ,
11271143 } ,
11281144 {
11291145 types : [ InputArgument . TYPE_STRING ] ,
0 commit comments