gojs

package
v1.0.0-pre.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 1, 2022 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ClearTimeoutEvent = stubFunction(functionClearTimeoutEvent)

ClearTimeoutEvent implements runtime.clearTimeoutEvent which supports runtime.notetsleepg used by runtime.signal_recv.

See https://github.com/golang/go/blob/go1.19/src/runtime/sys_wasm.s#L196

View Source
var CopyBytesToGo = spfunc.MustCallFromSP(false, wasm.NewGoFunc(
	functionCopyBytesToGo, functionCopyBytesToGo,
	[]string{"dstAddr", "dstLen", "src"},
	func(ctx context.Context, mod api.Module, dstAddr, dstLen, _ uint32, srcRef uint64) (n, ok uint32) {
		dst := mustRead(ctx, mod.Memory(), "dst", dstAddr, dstLen)
		v := loadValue(ctx, ref(srcRef))
		if src, isBuf := v.(*byteArray); isBuf {
			n = uint32(copy(dst, src.slice))
			ok = 1
		}
		return
	},
))

CopyBytesToGo copies a JavaScript managed byte array to linear memory. For example, this is used to read an HTTP response body.

Results

  • n is the count of bytes written.
  • ok is false if the src was not a uint8Array.

See https://github.com/golang/go/blob/go1.19/src/syscall/js/js.go#L569 and https://github.com/golang/go/blob/go1.19/misc/wasm/wasm_exec.js#L424-L433

View Source
var CopyBytesToJS = spfunc.MustCallFromSP(false, wasm.NewGoFunc(
	functionCopyBytesToJS, functionCopyBytesToJS,
	[]string{"dst", "srcAddr", "srcLen"},
	func(ctx context.Context, mod api.Module, dstRef uint64, srcAddr, srcLen, _ uint32) (n, ok uint32) {
		src := mustRead(ctx, mod.Memory(), "src", srcAddr, srcLen)
		v := loadValue(ctx, ref(dstRef))
		if dst, isBuf := v.(*byteArray); isBuf {
			if dst != nil {
				n = uint32(copy(dst.slice, src))
			}
			ok = 1
		}
		return
	},
))

CopyBytesToJS copies linear memory to a JavaScript managed byte array. For example, this is used to read an HTTP request body.

Results

  • n is the count of bytes written.
  • ok is false if the dst was not a uint8Array.

See https://github.com/golang/go/blob/go1.19/src/syscall/js/js.go#L583

https://github.com/golang/go/blob/go1.19/misc/wasm/wasm_exec.js#L438-L448
View Source
var Debug = stubFunction(functionDebug)

Debug has unknown use, so stubbed.

See https://github.com/golang/go/blob/go1.19/src/cmd/link/internal/wasm/asm.go#L133-L138

View Source
var FinalizeRef = spfunc.MustCallFromSP(false, wasm.NewGoFunc(
	functionFinalizeRef, functionFinalizeRef,
	[]string{"r"},
	func(ctx context.Context, mod api.Module, id uint32) {
		getState(ctx).values.decrement(id)
	},
))

FinalizeRef implements js.finalizeRef, which is used as a runtime.SetFinalizer on the given reference.

See https://github.com/golang/go/blob/go1.19/src/syscall/js/js.go#L61

View Source
var GetRandomData = spfunc.MustCallFromSP(false, wasm.NewGoFunc(
	functionGetRandomData, functionGetRandomData,
	[]string{"buf", "bufLen"},
	func(ctx context.Context, mod api.Module, buf, bufLen uint32) {
		randSource := mod.(*wasm.CallContext).Sys.RandSource()

		r := mustRead(ctx, mod.Memory(), "r", buf, bufLen)

		if n, err := randSource.Read(r); err != nil {
			panic(fmt.Errorf("RandSource.Read(r /* len=%d */) failed: %w", bufLen, err))
		} else if uint32(n) != bufLen {
			panic(fmt.Errorf("RandSource.Read(r /* len=%d */) read %d bytes", bufLen, n))
		}
	},
))

GetRandomData implements runtime.getRandomData, which initializes the seed for runtime.fastrand.

See https://github.com/golang/go/blob/go1.19/src/runtime/sys_wasm.s#L200

View Source
var NaN = math.NaN()
View Source
var Nanotime1 = spfunc.MustCallFromSP(false, wasm.NewGoFunc(
	functionNanotime1, functionNanotime1,
	[]string{},
	func(ctx context.Context, mod api.Module) int64 {
		return mod.(*wasm.CallContext).Sys.Nanotime(ctx)
	}))

Nanotime1 implements runtime.nanotime which supports time.Since.

See https://github.com/golang/go/blob/go1.19/src/runtime/sys_wasm.s#L184

View Source
var ResetMemoryDataView = &wasm.HostFunc{
	ExportNames: []string{functionResetMemoryDataView},
	Name:        functionResetMemoryDataView,
	ParamTypes:  []wasm.ValueType{wasm.ValueTypeI32},
	ParamNames:  []string{parameterSp},

	Code: &wasm.Code{IsHostFunction: true, Body: []byte{wasm.OpcodeEnd}},
}

ResetMemoryDataView signals wasm.OpcodeMemoryGrow happened, indicating any cached view of memory should be reset.

See https://github.com/golang/go/blob/go1.19/src/runtime/mem_js.go#L82

View Source
var ScheduleTimeoutEvent = stubFunction(functionScheduleTimeoutEvent)

ScheduleTimeoutEvent implements runtime.scheduleTimeoutEvent which supports runtime.notetsleepg used by runtime.signal_recv.

Unlike other most functions prefixed by "runtime.", this both launches a goroutine and invokes code compiled into wasm "resume".

See https://github.com/golang/go/blob/go1.19/src/runtime/sys_wasm.s#L192

View Source
var StringVal = spfunc.MustCallFromSP(false, wasm.NewGoFunc(
	functionStringVal, functionStringVal,
	[]string{"xAddr", "xLen"},
	func(ctx context.Context, mod api.Module, xAddr, xLen uint32) uint64 {
		x := string(mustRead(ctx, mod.Memory(), "x", xAddr, xLen))
		return storeRef(ctx, x)
	},
))

StringVal implements js.stringVal, which is used to load the string for `js.ValueOf(x)`. For example, this is used when setting HTTP headers.

See https://github.com/golang/go/blob/go1.19/src/syscall/js/js.go#L212 and https://github.com/golang/go/blob/go1.19/misc/wasm/wasm_exec.js#L305-L308

View Source
var ValueCall = spfunc.MustCallFromSP(true, wasm.NewGoFunc(
	functionValueCall, functionValueCall,
	[]string{"v", "mAddr", "mLen", "argsArray", "argsLen"},
	func(ctx context.Context, mod api.Module, vRef uint64, mAddr, mLen, argsArray, argsLen uint32) (xRef uint64, ok uint32, sp uint32) {
		this := ref(vRef)
		v := loadValue(ctx, this)
		m := string(mustRead(ctx, mod.Memory(), "m", mAddr, mLen))
		args := loadArgs(ctx, mod, argsArray, argsLen)

		if c, isCall := v.(jsCall); !isCall {
			panic(fmt.Errorf("TODO: valueCall(v=%v, m=%v, args=%v)", v, m, args))
		} else if result, err := c.call(ctx, mod, this, m, args...); err != nil {
			xRef = storeRef(ctx, err)
			ok = 0
		} else {
			xRef = storeRef(ctx, result)
			ok = 1
		}

		sp = refreshSP(mod)
		return
	},
))

ValueCall implements js.valueCall, which is used to call a js.Value function by name, ex. `document.Call("createElement", "div")`.

See https://github.com/golang/go/blob/go1.19/src/syscall/js/js.go#L394

https://github.com/golang/go/blob/go1.19/misc/wasm/wasm_exec.js#L343-L358
View Source
var ValueDelete = stubFunction(functionValueDelete)

ValueDelete is stubbed as it isn't used in Go's main source tree.

See https://github.com/golang/go/blob/go1.19/src/syscall/js/js.go#L321

View Source
var ValueGet = spfunc.MustCallFromSP(false, wasm.NewGoFunc(
	functionValueGet, functionValueGet,
	[]string{"v", "pAddr", "pLen"},
	func(ctx context.Context, mod api.Module, vRef uint64, pAddr, pLen uint32) uint64 {
		p := string(mustRead(ctx, mod.Memory(), "p", pAddr, pLen))
		v := loadValue(ctx, ref(vRef))

		var result interface{}
		if g, ok := v.(jsGet); ok {
			result = g.get(ctx, p)
		} else if e, ok := v.(error); ok {
			switch p {
			case "message":
				result = e.Error()
			case "code":
				result = mapJSError(e).Error()
			default:
				panic(fmt.Errorf("TODO: valueGet(v=%v, p=%s)", v, p))
			}
		} else {
			panic(fmt.Errorf("TODO: valueGet(v=%v, p=%s)", v, p))
		}

		xRef := storeRef(ctx, result)
		return xRef
	},
))

ValueGet implements js.valueGet, which is used to load a js.Value property by name, ex. `v.Get("address")`. Notably, this is used by js.handleEvent to get the pending event.

See https://github.com/golang/go/blob/go1.19/src/syscall/js/js.go#L295 and https://github.com/golang/go/blob/go1.19/misc/wasm/wasm_exec.js#L311-L316

View Source
var ValueIndex = spfunc.MustCallFromSP(false, wasm.NewGoFunc(
	functionValueIndex, functionValueIndex,
	[]string{"v", "i"},
	func(ctx context.Context, mod api.Module, vRef uint64, i uint32) (xRef uint64) {
		v := loadValue(ctx, ref(vRef))
		result := v.(*objectArray).slice[i]
		xRef = storeRef(ctx, result)
		return
	},
))

ValueIndex implements js.valueIndex, which is used to load a js.Value property by index, ex. `v.Index(0)`. Notably, this is used by js.handleEvent to read event arguments

See https://github.com/golang/go/blob/go1.19/src/syscall/js/js.go#L334 and https://github.com/golang/go/blob/go1.19/misc/wasm/wasm_exec.js#L331-L334

View Source
var ValueInstanceOf = stubFunction(functionValueInstanceOf)

ValueInstanceOf is stubbed as it isn't used in Go's main source tree.

See https://github.com/golang/go/blob/go1.19/src/syscall/js/js.go#L543

View Source
var ValueInvoke = stubFunction(functionValueInvoke)

ValueInvoke is stubbed as it isn't used in Go's main source tree.

See https://github.com/golang/go/blob/go1.19/src/syscall/js/js.go#L413

View Source
var ValueLength = spfunc.MustCallFromSP(false, wasm.NewGoFunc(
	functionValueLength, functionValueLength,
	[]string{"v"},
	func(ctx context.Context, mod api.Module, vRef uint64) uint32 {
		v := loadValue(ctx, ref(vRef))
		return uint32(len(v.(*objectArray).slice))
	},
))

ValueLength implements js.valueLength, which is used to load the length property of a value, ex. `array.length`.

See https://github.com/golang/go/blob/go1.19/src/syscall/js/js.go#L372 and https://github.com/golang/go/blob/go1.19/misc/wasm/wasm_exec.js#L396-L397

View Source
var ValueLoadString = spfunc.MustCallFromSP(false, wasm.NewGoFunc(
	functionValueLoadString, functionValueLoadString,
	[]string{"v", "bAddr", "bLen"},
	func(ctx context.Context, mod api.Module, vRef uint64, bAddr, bLen uint32) {
		v := loadValue(ctx, ref(vRef))
		s := valueString(v)
		b := mustRead(ctx, mod.Memory(), "b", bAddr, bLen)
		copy(b, s)
	},
))

ValueLoadString implements js.valueLoadString, which is used copy a string value for `o.String()`.

See https://github.com/golang/go/blob/go1.19/src/syscall/js/js.go#L533

https://github.com/golang/go/blob/go1.19/misc/wasm/wasm_exec.js#L410-L412
View Source
var ValueNew = spfunc.MustCallFromSP(true, wasm.NewGoFunc(
	functionValueNew, functionValueNew,
	[]string{"v", "argsArray", "argsLen"},
	func(ctx context.Context, mod api.Module, vRef uint64, argsArray, argsLen uint32) (xRef uint64, ok uint32, sp uint32) {
		args := loadArgs(ctx, mod, argsArray, argsLen)
		ref := ref(vRef)
		v := loadValue(ctx, ref)

		switch ref {
		case refArrayConstructor:
			result := &objectArray{}
			xRef = storeRef(ctx, result)
			ok = 1
		case refUint8ArrayConstructor:
			var result *byteArray
			if n, ok := args[0].(float64); ok {
				result = &byteArray{make([]byte, uint32(n))}
			} else if n, ok := args[0].(uint32); ok {
				result = &byteArray{make([]byte, n)}
			} else if b, ok := args[0].(*byteArray); ok {

				result = b
			} else {
				panic(fmt.Errorf("TODO: valueNew(v=%v, args=%v)", v, args))
			}
			xRef = storeRef(ctx, result)
			ok = 1
		case refObjectConstructor:
			result := &object{properties: map[string]interface{}{}}
			xRef = storeRef(ctx, result)
			ok = 1
		case refHttpHeadersConstructor:
			result := &headers{headers: http.Header{}}
			xRef = storeRef(ctx, result)
			ok = 1
		case refJsDateConstructor:
			xRef = uint64(refJsDate)
			ok = 1
		default:
			panic(fmt.Errorf("TODO: valueNew(v=%v, args=%v)", v, args))
		}

		sp = refreshSP(mod)
		return
	},
))

ValueNew implements js.valueNew, which is used to call a js.Value, ex. `array.New(2)`.

See https://github.com/golang/go/blob/go1.19/src/syscall/js/js.go#L432 and https://github.com/golang/go/blob/go1.19/misc/wasm/wasm_exec.js#L380-L391

View Source
var ValuePrepareString = spfunc.MustCallFromSP(false, wasm.NewGoFunc(
	functionValuePrepareString, functionValuePrepareString,
	[]string{"v"},
	func(ctx context.Context, mod api.Module, vRef uint64) (sRef uint64, sLen uint32) {
		v := loadValue(ctx, ref(vRef))
		s := valueString(v)
		sRef = storeRef(ctx, s)
		sLen = uint32(len(s))
		return
	},
))

ValuePrepareString implements js.valuePrepareString, which is used to load the string for `o.String()` (via js.jsString) for string, boolean and number types. Notably, http.Transport uses this in RoundTrip to coerce the URL to a string.

See https://github.com/golang/go/blob/go1.19/src/syscall/js/js.go#L531 and https://github.com/golang/go/blob/go1.19/misc/wasm/wasm_exec.js#L402-L405

View Source
var ValueSet = spfunc.MustCallFromSP(false, wasm.NewGoFunc(
	functionValueSet, functionValueSet,
	[]string{"v", "pAddr", "pLen", "x"},
	func(ctx context.Context, mod api.Module, vRef uint64, pAddr, pLen uint32, xRef uint64) {
		v := loadValue(ctx, ref(vRef))
		p := string(mustRead(ctx, mod.Memory(), "p", pAddr, pLen))
		x := loadValue(ctx, ref(xRef))
		if v == getState(ctx) {
			switch p {
			case "_pendingEvent":
				if x == nil {
					v.(*state)._pendingEvent = nil
					return
				}
			}
		} else if e, ok := v.(*event); ok {
			switch p {
			case "result":
				e.result = x
				return
			}
		} else if m, ok := v.(*object); ok {
			m.properties[p] = x
			return
		}
		panic(fmt.Errorf("TODO: valueSet(v=%v, p=%s, x=%v)", v, p, x))
	},
))

ValueSet implements js.valueSet, which is used to store a js.Value property by name, ex. `v.Set("address", a)`. Notably, this is used by js.handleEvent set the event result.

See https://github.com/golang/go/blob/go1.19/src/syscall/js/js.go#L309 and https://github.com/golang/go/blob/go1.19/misc/wasm/wasm_exec.js#L318-L322

View Source
var ValueSetIndex = stubFunction(functionValueSetIndex)

ValueSetIndex is stubbed as it is only used for js.ValueOf when the input is []interface{}, which doesn't appear to occur in Go's source tree.

See https://github.com/golang/go/blob/go1.19/src/syscall/js/js.go#L348

View Source
var Walltime = spfunc.MustCallFromSP(false, wasm.NewGoFunc(
	functionWalltime, functionWalltime,
	[]string{},
	func(ctx context.Context, mod api.Module) (sec int64, nsec int32) {
		return mod.(*wasm.CallContext).Sys.Walltime(ctx)
	}))

Walltime implements runtime.walltime which supports time.Now.

See https://github.com/golang/go/blob/go1.19/src/runtime/sys_wasm.s#L188

View Source
var WasmExit = spfunc.MustCallFromSP(false, wasm.NewGoFunc(
	functionWasmExit, functionWasmExit,
	[]string{"code"},
	func(ctx context.Context, mod api.Module, code int32) {
		getState(ctx).clear()
		_ = mod.CloseWithExitCode(ctx, uint32(code))
	},
))

WasmExit implements runtime.wasmExit which supports runtime.exit.

See https://github.com/golang/go/blob/go1.19/src/runtime/sys_wasm.go#L28

View Source
var WasmWrite = spfunc.MustCallFromSP(false, wasm.NewGoFunc(
	functionWasmWrite, functionWasmWrite,
	[]string{"code"},
	func(ctx context.Context, mod api.Module, fd, p, n uint32) {
		var writer io.Writer

		switch fd {
		case 1:
			writer = mod.(*wasm.CallContext).Sys.Stdout()
		case 2:
			writer = mod.(*wasm.CallContext).Sys.Stderr()
		default:

			panic(fmt.Errorf("unexpected fd %d", fd))
		}

		if _, err := writer.Write(mustRead(ctx, mod.Memory(), "p", p, n)); err != nil {
			panic(fmt.Errorf("error writing p: %w", err))
		}
	},
))

WasmWrite implements runtime.wasmWrite which supports runtime.write and runtime.writeErr. This implements `println`.

See https://github.com/golang/go/blob/go1.19/src/runtime/os_js.go#L29

Functions

func WithState

func WithState(ctx context.Context) context.Context

func WriteArgsAndEnviron

func WriteArgsAndEnviron(ctx context.Context, mod api.Module) (argc, argv uint32, err error)

WriteArgsAndEnviron writes arguments and environment variables to memory, so they can be read by main, Go compiles as the function export "run".

Types

type RoundTripperKey

type RoundTripperKey struct{}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL