How to use Runtime method of js Package

Best K6 code snippet using js.Runtime

patch.go

Source:patch.go Github

copy

Full Screen

1/*2Copyright 2017 The Kubernetes Authors.3Licensed under the Apache License, Version 2.0 (the "License");4you may not use this file except in compliance with the License.5You may obtain a copy of the License at6 http://www.apache.org/licenses/LICENSE-2.07Unless required by applicable law or agreed to in writing, software8distributed under the License is distributed on an "AS IS" BASIS,9WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.10See the License for the specific language governing permissions and11limitations under the License.12*/13package handlers14import (15 "fmt"16 "k8s.io/apimachinery/pkg/conversion/unstructured"17 "k8s.io/apimachinery/pkg/runtime"18 "k8s.io/apimachinery/pkg/types"19 "k8s.io/apimachinery/pkg/util/json"20 "k8s.io/apimachinery/pkg/util/strategicpatch"21 "github.com/evanphx/json-patch"22)23// patchObjectJSON patches the <originalObject> with <patchJS> and stores24// the result in <objToUpdate>.25// Currently it also returns the original and patched objects serialized to26// JSONs (this may not be needed once we can apply patches at the27// map[string]interface{} level).28func patchObjectJSON(29 patchType types.PatchType,30 codec runtime.Codec,31 originalObject runtime.Object,32 patchJS []byte,33 objToUpdate runtime.Object,34 versionedObj runtime.Object,35) (originalObjJS []byte, patchedObjJS []byte, retErr error) {36 js, err := runtime.Encode(codec, originalObject)37 if err != nil {38 return nil, nil, err39 }40 originalObjJS = js41 switch patchType {42 case types.JSONPatchType:43 patchObj, err := jsonpatch.DecodePatch(patchJS)44 if err != nil {45 return nil, nil, err46 }47 if patchedObjJS, err = patchObj.Apply(originalObjJS); err != nil {48 return nil, nil, err49 }50 case types.MergePatchType:51 if patchedObjJS, err = jsonpatch.MergePatch(originalObjJS, patchJS); err != nil {52 return nil, nil, err53 }54 case types.StrategicMergePatchType:55 if patchedObjJS, err = strategicpatch.StrategicMergePatch(originalObjJS, patchJS, versionedObj); err != nil {56 return nil, nil, err57 }58 default:59 // only here as a safety net - go-restful filters content-type60 return nil, nil, fmt.Errorf("unknown Content-Type header for patch: %v", patchType)61 }62 if err := runtime.DecodeInto(codec, patchedObjJS, objToUpdate); err != nil {63 return nil, nil, err64 }65 return66}67// strategicPatchObject applies a strategic merge patch of <patchJS> to68// <originalObject> and stores the result in <objToUpdate>.69// It additionally returns the map[string]interface{} representation of the70// <originalObject> and <patchJS>.71// NOTE: Both <originalObject> and <objToUpdate> are supposed to be versioned.72func strategicPatchObject(73 codec runtime.Codec,74 defaulter runtime.ObjectDefaulter,75 originalObject runtime.Object,76 patchJS []byte,77 objToUpdate runtime.Object,78 versionedObj runtime.Object,79) error {80 originalObjMap, err := unstructured.DefaultConverter.ToUnstructured(originalObject)81 if err != nil {82 return err83 }84 patchMap := make(map[string]interface{})85 if err := json.Unmarshal(patchJS, &patchMap); err != nil {86 return err87 }88 if err := applyPatchToObject(codec, defaulter, originalObjMap, patchMap, objToUpdate, versionedObj); err != nil {89 return err90 }91 return nil92}93// applyPatchToObject applies a strategic merge patch of <patchMap> to94// <originalMap> and stores the result in <objToUpdate>.95// NOTE: <objToUpdate> must be a versioned object.96func applyPatchToObject(97 codec runtime.Codec,98 defaulter runtime.ObjectDefaulter,99 originalMap map[string]interface{},100 patchMap map[string]interface{},101 objToUpdate runtime.Object,102 versionedObj runtime.Object,103) error {104 patchedObjMap, err := strategicpatch.StrategicMergeMapPatch(originalMap, patchMap, versionedObj)105 if err != nil {106 return err107 }108 // Rather than serialize the patched map to JSON, then decode it to an object, we go directly from a map to an object109 if err := unstructured.DefaultConverter.FromUnstructured(patchedObjMap, objToUpdate); err != nil {110 return err111 }112 // Decoding from JSON to a versioned object would apply defaults, so we do the same here113 defaulter.Default(objToUpdate)114 return nil115}...

Full Screen

Full Screen

runtime.go

Source:runtime.go Github

copy

Full Screen

...6 "github.com/xuperchain/xupercore/xvm/runtime/go/js"7 "github.com/xuperchain/xupercore/xvm/runtime/go/js/fs"8)9const (10 goRuntimeKey = "goruntime"11)12// Runtime implements the runtime needed to run wasm code compiled by go toolchain13type Runtime struct {14 exitcode int3215 exited bool16 global *js.Global17 jsvm *js.VM18 ctx exec.Context19 timeOrigin time.Time20}21// RegisterRuntime 用于向exec.Context里面注册一个初始化好的js Runtime22func RegisterRuntime(ctx exec.Context) *Runtime {23 rt := &Runtime{24 global: js.NewGlobal(),25 timeOrigin: time.Now(),26 ctx: ctx,27 }28 ctx.SetUserData(goRuntimeKey, rt)29 jsmem := js.NewMemory(func() []byte {30 return rt.ctx.Memory()31 })32 rt.jsvm = js.NewVM(&js.VMConfig{33 Memory: jsmem,34 Global: rt.global,35 })36 rt.global.Register("Fs", fs.NewFS())37 return rt38}39func (rt *Runtime) wasmExit(code int32) {40 rt.exitcode = code41 rt.exited = true42}43func (rt *Runtime) wasmWrite(fd int64, p int64, n int32) {44 codec := exec.NewCodec(rt.ctx)45 if fd == 1 || fd == 2 {46 debug.Write(rt.ctx, codec.Bytes(uint32(p), uint32(n)))47 }48}49func (rt *Runtime) nanotime() int64 {50 return 151}52func (rt *Runtime) walltime() (int64, int32) {53 return 0, 054}55// Exited will be true if runtime.wasmExit has been called56func (rt *Runtime) Exited() bool {57 return rt.exited58}59// ExitCode return the exit code of go application60func (rt *Runtime) ExitCode() int32 {61 return rt.exitcode62}63// WaitTimer waiting for timeout of timers set by go runtime in wasm64func (rt *Runtime) WaitTimer() {65}66func (rt *Runtime) scheduleCallback(delay int64) int32 {67 return 068}69func (rt *Runtime) clearScheduledCallback(id int32) {70}71func (rt *Runtime) getRandomData(r []byte) {72 for i := range r {73 r[i] = 074 }75}76func (rt *Runtime) debug(v int64) {77}78func (rt *Runtime) syscallJsValueGet(ref js.Ref, name string) (ret js.Ref) {79 defer rt.jsvm.CatchException(&ret)80 ret = rt.jsvm.Property(ref, name)81 return ret82}83func (rt *Runtime) syscallJsValueSet(ref js.Ref, name string, value js.Ref) {84}85func (rt *Runtime) syscallJsValueNew(ref js.Ref, args []js.Ref) (ret js.Ref, ok bool) {86 defer rt.jsvm.CatchException(&ret)87 return rt.jsvm.New(ref, args), true88}89func (rt *Runtime) syscallJsValueCall(ref js.Ref, method string, args []js.Ref) (ret js.Ref, ok bool) {90 defer rt.jsvm.CatchException(&ret)91 return rt.jsvm.Call(ref, method, args), true92}93func (rt *Runtime) syscallJsValueInvoke(ref js.Ref, args []js.Ref) (ret js.Ref, ok bool) {94 defer rt.jsvm.CatchException(&ret)95 return rt.jsvm.Invoke(ref, args), true96}97func (rt *Runtime) syscallJsValuePrepareString(ref js.Ref) (js.Ref, int64) {98 v := rt.jsvm.Value(ref)99 if v == nil {100 return rt.jsvm.Exception(js.ExceptionRefNotFound(ref)), 0101 }102 str := v.String()103 return rt.jsvm.Store(str), int64(len(str))104}105func (rt *Runtime) syscallJsValueLoadString(ref js.Ref, b []byte) {106 v := rt.jsvm.Value(ref)107 if v == nil {108 return109 }110 str := v.String()111 copy(b, str)112}113func (rt *Runtime) syscallJsStringVal(value string) js.Ref {114 return rt.jsvm.Store(value)115}116func (rt *Runtime) syscallJsValueIndex(ref js.Ref, idx int64) js.Ref {117 return 0118}119func (rt *Runtime) syscallJsValueSetIndex(ref js.Ref, idx int64, x js.Ref) {120}121func (rt *Runtime) syscallJsValueLength(ref js.Ref) int64 {122 return 0123}124func (rt *Runtime) syscallJsValueInstanceOf(v, t js.Ref) {125}126func (rt *Runtime) syscallJsCopyBytesToGo(dst []byte, src js.Ref) (int64, bool) {127 v := rt.jsvm.Value(src)128 if v == nil {129 return 0, false130 }131 slice, err := v.Bytes()132 if err != nil {133 return 0, false134 }135 n := copy(dst, slice)136 return int64(n), true137}138func (rt *Runtime) syscallJsCopyBytesToJS(dst js.Ref, src []byte) (int64, bool) {139 v := rt.jsvm.Value(dst)140 if v == nil {141 return 0, false142 }143 slice, err := v.Bytes()144 if err != nil {145 return 0, false146 }147 n := copy(slice, src)148 return int64(n), true149}...

Full Screen

Full Screen

resolver.go

Source:resolver.go Github

copy

Full Screen

...12func (r *resolver) resolveFunc(module, name string) (interface{}, bool) {13 fullname := module + "::" + name14 switch fullname {15 case "go::debug":16 return (*Runtime).debug, true17 case "go::runtime.wasmExit":18 return (*Runtime).wasmExit, true19 case "go::runtime.wasmWrite":20 return (*Runtime).wasmWrite, true21 case "go::runtime.nanotime":22 return (*Runtime).nanotime, true23 case "go::runtime.walltime":24 return (*Runtime).walltime, true25 case "go::runtime.getRandomData":26 return (*Runtime).getRandomData, true27 case "go::runtime.scheduleCallback": // for go.1128 return (*Runtime).scheduleCallback, true29 case "go::runtime.clearScheduledCallback": // for go.1130 return (*Runtime).clearScheduledCallback, true31 case "go::runtime.scheduleTimeoutEvent":32 return (*Runtime).scheduleCallback, true33 case "go::runtime.clearTimeoutEvent":34 return (*Runtime).clearScheduledCallback, true35 case "go::syscall/js.valueGet":36 return (*Runtime).syscallJsValueGet, true37 case "go::syscall/js.valueSet":38 return (*Runtime).syscallJsValueSet, true39 case "go::syscall/js.valueNew":40 return (*Runtime).syscallJsValueNew, true41 case "go::syscall/js.valuePrepareString":42 return (*Runtime).syscallJsValuePrepareString, true43 case "go::syscall/js.valueCall":44 return (*Runtime).syscallJsValueCall, true45 case "go::syscall/js.valueInvoke":46 return (*Runtime).syscallJsValueInvoke, true47 case "go::syscall/js.stringVal":48 return (*Runtime).syscallJsStringVal, true49 case "go::syscall/js.valueLoadString":50 return (*Runtime).syscallJsValueLoadString, true51 case "go::syscall/js.valueLength":52 return (*Runtime).syscallJsValueLength, true53 case "go::syscall/js.valueIndex":54 return (*Runtime).syscallJsValueIndex, true55 case "go::syscall/js.valueSetIndex":56 return (*Runtime).syscallJsValueSetIndex, true57 case "go::syscall/js.valueInstanceOf":58 return (*Runtime).syscallJsValueInstanceOf, true59 case "go::syscall/js.copyBytesToGo": // for go1.1360 return (*Runtime).syscallJsCopyBytesToGo, true61 case "go::syscall/js.copyBytesToJS": // for go1.1362 return (*Runtime).syscallJsCopyBytesToJS, true63 }64 return nil, false65}66func (r *resolver) ResolveFunc(module, name string) (interface{}, bool) {67 ifunc, ok := r.resolveFunc(module, name)68 if !ok {69 return nil, false70 }71 Type, Value := reflect.TypeOf(ifunc), reflect.ValueOf(ifunc)72 realFunc := func(ctx exec.Context, sp uint32) uint32 {73 rt := ctx.GetUserData(goRuntimeKey).(*Runtime)74 mem := ctx.Memory()75 dec := NewDecoder(mem, sp+8)76 args := []reflect.Value{reflect.ValueOf(rt)}77 for i := 1; i < Type.NumIn(); i++ {78 argtype := Type.In(i)79 ref := reflect.New(argtype)80 dec.Decode(ref)81 args = append(args, ref.Elem())82 }83 rets := Value.Call(args)84 enc := NewEncoder(mem, dec.Offset())85 for i := 0; i < len(rets); i++ {86 ret := rets[i]87 enc.Encode(ret)...

Full Screen

Full Screen

Runtime

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 jsRuntime := js.Global().Get("Runtime")4 if jsRuntime.IsNull() {5 fmt.Println("Runtime is not defined")6 }7 jsRuntime.Invoke("print", "Hello, WebAssembly!")8}9import (10func main() {11 jsRuntime := js.Global().Get("Runtime")12 if jsRuntime.IsNull() {13 fmt.Println("Runtime is not defined")14 }15 jsRuntime.Invoke("print", "Hello, WebAssembly!")16}17import (18func main() {19 jsRuntime := js.Global().Get("Runtime")20 if jsRuntime.IsNull() {21 fmt.Println("Runtime is not defined")22 }23 jsRuntime.Invoke("print", "Hello, WebAssembly!")24}25import (26func main() {27 jsRuntime := js.Global().Get("Runtime")28 if jsRuntime.IsNull() {29 fmt.Println("Runtime is not defined")30 }31 jsRuntime.Invoke("print", "Hello, WebAssembly!")32}33import (34func main() {35 jsRuntime := js.Global().Get("Runtime")36 if jsRuntime.IsNull() {37 fmt.Println("Runtime is not defined")38 }39 jsRuntime.Invoke("print", "Hello, WebAssembly!")40}41import (

Full Screen

Full Screen

Runtime

Using AI Code Generation

copy

Full Screen

1func main() {2 js.Global().Set("hello", js.FuncOf(hello))3 js.Global().Set("hello2", js.FuncOf(hello2))4}5func hello(this js.Value, args []js.Value) interface{} {6 rt := js.Global().Get("Runtime")7 obj := rt.New()8 obj2 := rt.New()9 obj3 := rt.New()10 obj4 := rt.New()11 obj5 := rt.New()12 obj6 := rt.New()13 obj7 := rt.New()14 obj8 := rt.New()15 obj9 := rt.New()16 obj10 := rt.New()17 obj11 := rt.New()18 obj12 := rt.New()19 obj13 := rt.New()20 obj14 := rt.New()21 obj15 := rt.New()22 obj16 := rt.New()23 obj17 := rt.New()24 obj18 := rt.New()25 obj19 := rt.New()26 obj20 := rt.New()27 obj21 := rt.New()28 obj22 := rt.New()29 obj23 := rt.New()30 obj24 := rt.New()31 obj25 := rt.New()32 obj26 := rt.New()33 obj27 := rt.New()

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run K6 automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used method in

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful