cli: adds -hostlogging=filesystem for diagnosing problems (#966)

Signed-off-by: Adrian Cole <adrian@tetrate.io>
This commit is contained in:
Crypt Keeper
2022-12-28 11:38:24 +08:00
committed by GitHub
parent d874ebbe48
commit 921df7e7a6
24 changed files with 1111 additions and 388 deletions

View File

@@ -25,6 +25,16 @@ func getState(ctx context.Context) *state {
return ctx.Value(stateKey{}).(*state)
}
// GetLastEventArgs implements goos.GetLastEventArgs
func GetLastEventArgs(ctx context.Context) []interface{} {
if ls := ctx.Value(stateKey{}).(*state)._lastEvent; ls != nil {
if args := ls.args; args != nil {
return args.slice
}
}
return nil
}
type event struct {
// id is the funcWrapper.id
id uint32
@@ -51,11 +61,11 @@ var (
NaN = math.NaN()
)
// loadValue reads up to 8 bytes at the memory offset `addr` to return the
// LoadValue reads up to 8 bytes at the memory offset `addr` to return the
// value written by storeValue.
//
// See https://github.com/golang/go/blob/go1.19/misc/wasm/wasm_exec.js#L122-L133
func loadValue(ctx context.Context, ref goos.Ref) interface{} { // nolint
func LoadValue(ctx context.Context, ref goos.Ref) interface{} { //nolint
switch ref {
case 0:
return undefined
@@ -73,25 +83,25 @@ func loadValue(ctx context.Context, ref goos.Ref) interface{} { // nolint
return getState(ctx).valueGlobal
case goos.RefJsGo:
return getState(ctx)
case refObjectConstructor:
case goos.RefObjectConstructor:
return objectConstructor
case refArrayConstructor:
case goos.RefArrayConstructor:
return arrayConstructor
case refJsProcess:
case goos.RefJsProcess:
return jsProcess
case refJsfs:
case goos.RefJsfs:
return jsfs
case refJsfsConstants:
case goos.RefJsfsConstants:
return jsfsConstants
case refUint8ArrayConstructor:
case goos.RefUint8ArrayConstructor:
return uint8ArrayConstructor
case refJsCrypto:
case goos.RefJsCrypto:
return jsCrypto
case refJsDateConstructor:
case goos.RefJsDateConstructor:
return jsDateConstructor
case refJsDate:
case goos.RefJsDate:
return jsDate
case refHttpHeadersConstructor:
case goos.RefHttpHeadersConstructor:
return headersConstructor
default:
if f, ok := ref.ParseFloat(); ok { // numbers are passed through as a Ref
@@ -106,7 +116,7 @@ func loadValue(ctx context.Context, ref goos.Ref) interface{} { // nolint
// Any side effects besides memory must be cleaned up on wasmExit.
//
// See https://github.com/golang/go/blob/go1.19/misc/wasm/wasm_exec.js#L135-L183
func storeRef(ctx context.Context, v interface{}) goos.Ref { // nolint
func storeRef(ctx context.Context, v interface{}) goos.Ref { //nolint
// allow-list because we control all implementations
if v == undefined {
return goos.RefValueUndefined
@@ -134,25 +144,25 @@ func storeRef(ctx context.Context, v interface{}) goos.Ref { // nolint
} else if _, ok := v.(string); ok {
id := getState(ctx).values.increment(v)
return goos.ValueRef(id, goos.TypeFlagString)
} else if ui, ok := v.(uint32); ok {
if ui == 0 {
return goos.RefValueZero
}
// numbers are encoded as float and passed through as a Ref
return goos.Ref(api.EncodeF64(float64(ui)))
} else if u, ok := v.(uint64); ok {
// float is already encoded as a uint64, doesn't need to be stored.
return goos.Ref(u)
} else if u32, ok := v.(uint32); ok {
return toFloatRef(float64(u32))
} else if u64, ok := v.(uint64); ok {
return toFloatRef(float64(u64))
} else if f64, ok := v.(float64); ok {
if f64 == 0 {
return goos.RefValueZero
}
return goos.Ref(api.EncodeF64(f64))
return toFloatRef(f64)
}
id := getState(ctx).values.increment(v)
return goos.ValueRef(id, goos.TypeFlagObject)
}
func toFloatRef(f float64) goos.Ref {
if f == 0 {
return goos.RefValueZero
}
// numbers are encoded as float and passed through as a Ref
return goos.Ref(api.EncodeF64(f))
}
type values struct {
// Below is needed to avoid exhausting the ID namespace finalizeRef reclaims
// See https://go-review.googlesource.com/c/go/+/203600
@@ -164,7 +174,7 @@ type values struct {
}
func (j *values) get(id uint32) interface{} {
index := id - nextID
index := id - goos.NextID
if index >= uint32(len(j.values)) {
panic(fmt.Errorf("id %d is out of range %d", id, len(j.values)))
}
@@ -183,15 +193,15 @@ func (j *values) increment(v interface{}) uint32 {
j.ids[v] = id
}
j.goRefCounts[id]++
return id + nextID
return id + goos.NextID
}
func (j *values) decrement(id uint32) {
// Special IDs are not refcounted.
if id < nextID {
// Special IDs are not goos.Refcounted.
if id < goos.NextID {
return
}
id -= nextID
id -= goos.NextID
j.goRefCounts[id]--
if j.goRefCounts[id] == 0 {
j.values[id] = nil
@@ -204,6 +214,8 @@ func (j *values) decrement(id uint32) {
type state struct {
values *values
_pendingEvent *event
// _lastEvent was the last _pendingEvent value
_lastEvent *event
valueGlobal *jsVal
@@ -237,6 +249,7 @@ func (s *state) clear() {
}
s.values.idPool = s.values.idPool[:0]
s._pendingEvent = nil
s._lastEvent = nil
}
func toInt64(arg interface{}) int64 {
@@ -258,7 +271,7 @@ func toUint32(arg interface{}) uint32 {
}
// valueString returns the string form of JavaScript string, boolean and number types.
func valueString(v interface{}) string { // nolint
func valueString(v interface{}) string { //nolint
if s, ok := v.(string); ok {
return s
} else {