MCPcopy Create free account
hub / github.com/dop251/goja / ExampleObject_SetSymbol

Function ExampleObject_SetSymbol

runtime_test.go:2252–2292  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

2250}
2251
2252func ExampleObject_SetSymbol() {
2253 type IterResult struct {
2254 Done bool
2255 Value Value
2256 }
2257
2258 vm := New()
2259 vm.SetFieldNameMapper(UncapFieldNameMapper()) // to use IterResult
2260
2261 o := vm.NewObject()
2262 o.SetSymbol(SymIterator, func() *Object {
2263 count := 0
2264 iter := vm.NewObject()
2265 iter.Set("next", func() IterResult {
2266 if count < 10 {
2267 count++
2268 return IterResult{
2269 Value: vm.ToValue(count),
2270 }
2271 }
2272 return IterResult{
2273 Done: true,
2274 }
2275 })
2276 return iter
2277 })
2278 vm.Set("o", o)
2279
2280 res, err := vm.RunString(`
2281 var acc = "";
2282 for (var v of o) {
2283 acc += v + " ";
2284 }
2285 acc;
2286 `)
2287 if err != nil {
2288 panic(err)
2289 }
2290 fmt.Println(res)
2291 // Output: 1 2 3 4 5 6 7 8 9 10
2292}
2293
2294func ExampleRuntime_NewArray() {
2295 vm := New()

Callers

nothing calls this directly

Calls 9

SetSymbolMethod · 0.95
SetMethod · 0.95
NewFunction · 0.85
UncapFieldNameMapperFunction · 0.85
SetFieldNameMapperMethod · 0.80
NewObjectMethod · 0.80
ToValueMethod · 0.80
RunStringMethod · 0.80
SetMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…