wa-lang.org/wazero@v1.0.2/internal/gojs/builtin.go (about)

     1  package gojs
     2  
     3  import (
     4  	"net/http"
     5  )
     6  
     7  const (
     8  	// predefined
     9  
    10  	idValueNaN uint32 = iota
    11  	idValueZero
    12  	idValueNull
    13  	idValueTrue
    14  	idValueFalse
    15  	idValueGlobal
    16  	idJsGo
    17  
    18  	// The below are derived from analyzing `*_js.go` source.
    19  	idObjectConstructor
    20  	idArrayConstructor
    21  	idJsProcess
    22  	idJsfs
    23  	idJsfsConstants
    24  	idUint8ArrayConstructor
    25  	idJsCrypto
    26  	idJsDateConstructor
    27  	idJsDate
    28  	idHttpFetch
    29  	idHttpHeaders
    30  	nextID
    31  )
    32  
    33  const (
    34  	refValueUndefined         = ref(0)
    35  	refValueNaN               = (nanHead|ref(typeFlagNone))<<32 | ref(idValueNaN)
    36  	refValueZero              = (nanHead|ref(typeFlagNone))<<32 | ref(idValueZero)
    37  	refValueNull              = (nanHead|ref(typeFlagNone))<<32 | ref(idValueNull)
    38  	refValueTrue              = (nanHead|ref(typeFlagNone))<<32 | ref(idValueTrue)
    39  	refValueFalse             = (nanHead|ref(typeFlagNone))<<32 | ref(idValueFalse)
    40  	refValueGlobal            = (nanHead|ref(typeFlagObject))<<32 | ref(idValueGlobal)
    41  	refJsGo                   = (nanHead|ref(typeFlagObject))<<32 | ref(idJsGo)
    42  	refObjectConstructor      = (nanHead|ref(typeFlagFunction))<<32 | ref(idObjectConstructor)
    43  	refArrayConstructor       = (nanHead|ref(typeFlagFunction))<<32 | ref(idArrayConstructor)
    44  	refJsProcess              = (nanHead|ref(typeFlagObject))<<32 | ref(idJsProcess)
    45  	refJsfs                   = (nanHead|ref(typeFlagObject))<<32 | ref(idJsfs)
    46  	refJsfsConstants          = (nanHead|ref(typeFlagObject))<<32 | ref(idJsfsConstants)
    47  	refUint8ArrayConstructor  = (nanHead|ref(typeFlagFunction))<<32 | ref(idUint8ArrayConstructor)
    48  	refJsCrypto               = (nanHead|ref(typeFlagFunction))<<32 | ref(idJsCrypto)
    49  	refJsDateConstructor      = (nanHead|ref(typeFlagFunction))<<32 | ref(idJsDateConstructor)
    50  	refJsDate                 = (nanHead|ref(typeFlagObject))<<32 | ref(idJsDate)
    51  	refHttpFetch              = (nanHead|ref(typeFlagFunction))<<32 | ref(idHttpFetch)
    52  	refHttpHeadersConstructor = (nanHead|ref(typeFlagFunction))<<32 | ref(idHttpHeaders)
    53  )
    54  
    55  // newJsGlobal = js.Global() // js.go init
    56  func newJsGlobal(rt http.RoundTripper) *jsVal {
    57  	var fetchProperty interface{} = undefined
    58  	if rt != nil {
    59  		fetchProperty = refHttpFetch
    60  	}
    61  	return newJsVal(refValueGlobal, "global").
    62  		addProperties(map[string]interface{}{
    63  			"Object":          objectConstructor,
    64  			"Array":           arrayConstructor,
    65  			"crypto":          jsCrypto,
    66  			"Uint8Array":      uint8ArrayConstructor,
    67  			"fetch":           fetchProperty,
    68  			"AbortController": undefined,
    69  			"Headers":         headersConstructor,
    70  			"process":         jsProcess,
    71  			"fs":              jsfs,
    72  			"Date":            jsDateConstructor,
    73  		}).
    74  		addFunction("fetch", &fetch{})
    75  }
    76  
    77  var (
    78  	// Values below are not built-in, but verifiable by looking at Go's source.
    79  	// When marked "XX.go init", these are eagerly referenced during syscall.init
    80  
    81  	// jsGo is not a constant
    82  
    83  	// objectConstructor is used by js.ValueOf to make `map[string]any`.
    84  	//	Get("Object") // js.go init
    85  	objectConstructor = newJsVal(refObjectConstructor, "Object")
    86  
    87  	// arrayConstructor is used by js.ValueOf to make `[]any`.
    88  	//	Get("Array") // js.go init
    89  	arrayConstructor = newJsVal(refArrayConstructor, "Array")
    90  
    91  	// jsProcess = js.Global().Get("process") // fs_js.go init
    92  	jsProcess = newJsVal(refJsProcess, "process").
    93  			addProperties(map[string]interface{}{
    94  			"pid":  float64(1),   // Get("pid").Int() in syscall_js.go for syscall.Getpid
    95  			"ppid": refValueZero, // Get("ppid").Int() in syscall_js.go for syscall.Getppid
    96  		}).
    97  		addFunction("cwd", &cwd{}).                     // syscall.Cwd in fs_js.go
    98  		addFunction("chdir", &chdir{}).                 // syscall.Chdir in fs_js.go
    99  		addFunction("getuid", &returnZero{}).           // syscall.Getuid in syscall_js.go
   100  		addFunction("getgid", &returnZero{}).           // syscall.Getgid in syscall_js.go
   101  		addFunction("geteuid", &returnZero{}).          // syscall.Geteuid in syscall_js.go
   102  		addFunction("getgroups", &returnSliceOfZero{}). // syscall.Getgroups in syscall_js.go
   103  		addFunction("umask", &returnArg0{})             // syscall.Umask in syscall_js.go
   104  
   105  	// uint8ArrayConstructor = js.Global().Get("Uint8Array")
   106  	//	// fs_js.go, rand_js.go, roundtrip_js.go init
   107  	//
   108  	// It has only one invocation pattern: `buf := uint8Array.New(len(b))`
   109  	uint8ArrayConstructor = newJsVal(refUint8ArrayConstructor, "Uint8Array")
   110  )