MCPcopy Create free account
hub / github.com/buke/quickjs-go / resolveClassIDFromInheritance

Method resolveClassIDFromInheritance

value.go:1344–1394  ·  view source on GitHub ↗

resolveClassIDFromInheritance attempts to resolve classID by checking if this constructor extends a registered class and should use the parent's classID

()

Source from the content-addressed store, hash-verified

1342// resolveClassIDFromInheritance attempts to resolve classID by checking if this constructor
1343// extends a registered class and should use the parent's classID
1344func (v *Value) resolveClassIDFromInheritance() (uint32, bool) {
1345 // Simple and efficient approach: use JavaScript to traverse the prototype chain
1346 script := `
1347 (function(child) {
1348 // Walk up the prototype chain and collect all parent constructors
1349 let constructors = [];
1350 let current = child;
1351
1352 // Traverse up to 10 levels to prevent infinite loops
1353 for (let i = 0; i < 10; i++) {
1354 if (!current || !current.prototype) break;
1355
1356 let parentProto = Object.getPrototypeOf(current.prototype);
1357 if (!parentProto || parentProto === Object.prototype) break;
1358
1359 let parentConstructor = parentProto.constructor;
1360 if (!parentConstructor || parentConstructor === current) break;
1361
1362 constructors.push(parentConstructor);
1363 current = parentConstructor;
1364 }
1365
1366 return constructors;
1367 })
1368 `
1369
1370 traverser := v.ctx.Eval(script)
1371 defer traverser.Free()
1372
1373 // Get all parent constructors
1374 undefinedVal := v.ctx.NewUndefined()
1375 defer undefinedVal.Free()
1376 parents := traverser.Execute(undefinedVal, v)
1377 defer parents.Free()
1378
1379 // Check each parent to see if it's registered
1380 lengthVal := parents.Get("length")
1381 defer lengthVal.Free()
1382
1383 length := int(lengthVal.ToInt32())
1384 for i := 0; i < length; i++ {
1385 parent := parents.GetIdx(int64(i))
1386 defer parent.Free()
1387
1388 if classID, exists := getConstructorClassID(v.ctx, parent.ref); exists {
1389 return classID, true
1390 }
1391 }
1392
1393 return 0, false
1394}

Callers 1

goClassConstructorProxyFunction · 0.95

Calls 8

getConstructorClassIDFunction · 0.85
EvalMethod · 0.80
NewUndefinedMethod · 0.80
ExecuteMethod · 0.80
GetMethod · 0.80
ToInt32Method · 0.80
GetIdxMethod · 0.80
FreeMethod · 0.45

Tested by

no test coverage detected