@ -202,26 +202,31 @@
return ;
return ;
}
}
let ref = this . _ refs . get ( v ) ;
let id = this . _ ids . get ( v ) ;
if ( ref === undefined ) {
if ( id === undefined ) {
ref = this . _ values . length ;
id = this . _ idPool . pop ( ) ;
this . _ values . push ( v ) ;
if ( id === undefined ) {
this . _ refs . set ( v , ref ) ;
id = this . _ values . length ;
}
this . _ values [ id ] = v ;
this . _ goRefCounts [ id ] = 0 ;
this . _ ids . set ( v , id ) ;
}
}
let typeFlag = 0 ;
this . _ goRefCounts [ id ] ++ ;
let typeFlag = 1 ;
switch ( typeof v ) {
switch ( typeof v ) {
case "string" :
case "string" :
typeFlag = 1 ;
typeFlag = 2 ;
break ;
break ;
case "symbol" :
case "symbol" :
typeFlag = 2 ;
typeFlag = 3 ;
break ;
break ;
case "function" :
case "function" :
typeFlag = 3 ;
typeFlag = 4 ;
break ;
break ;
}
}
mem ( ) . setUint32 ( addr + 4 , nanHead | typeFlag , true ) ;
mem ( ) . setUint32 ( addr + 4 , nanHead | typeFlag , true ) ;
mem ( ) . setUint32 ( addr , ref , true ) ;
mem ( ) . setUint32 ( addr , id , true ) ;
}
}
const loadSlice = ( array , len , cap ) => {
const loadSlice = ( array , len , cap ) => {
@ -284,6 +289,13 @@
setTimeout ( this . _ inst . exports . go_scheduler , timeout ) ;
setTimeout ( this . _ inst . exports . go_scheduler , timeout ) ;
} ,
} ,
// func finalizeRef(v ref)
"syscall/js.finalizeRef" : ( sp ) => {
// Note: TinyGo does not support finalizers so this should never be
// called.
console . error ( 'syscall/js.finalizeRef not implemented' ) ;
} ,
// func stringVal(value string) ref
// func stringVal(value string) ref
"syscall/js.stringVal" : ( ret_ptr , value_ptr , value_len ) => {
"syscall/js.stringVal" : ( ret_ptr , value_ptr , value_len ) => {
const s = loadString ( value_ptr , value_len ) ;
const s = loadString ( value_ptr , value_len ) ;
@ -405,7 +417,7 @@
async run ( instance ) {
async run ( instance ) {
this . _ inst = instance ;
this . _ inst = instance ;
this . _ values = [ // TODO: garbage collection
this . _ values = [ // JS values that Go currently has references to, indexed by reference id
NaN ,
NaN ,
0 ,
0 ,
null ,
null ,
@ -414,8 +426,10 @@
global ,
global ,
this ,
this ,
] ;
] ;
this . _ refs = new Map ( ) ;
this . _ goRefCounts = [ ] ; // number of references that Go has to a JS value, indexed by reference id
this . exited = false ;
this . _ ids = new Map ( ) ; // mapping from JS values to reference ids
this . _ idPool = [ ] ; // unused ids that have been garbage collected
this . exited = false ; // whether the Go program has exited
const mem = new DataView ( this . _ inst . exports . memory . buffer )
const mem = new DataView ( this . _ inst . exports . memory . buffer )