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

Method construct

func.go:349–383  ·  view source on GitHub ↗
(args []Value, newTarget *Object)

Source from the content-addressed store, hash-verified

347}
348
349func (f *classFuncObject) construct(args []Value, newTarget *Object) *Object {
350 if newTarget == nil {
351 newTarget = f.val
352 }
353 if f.prg == nil {
354 instance := f.createInstance(args, newTarget)
355 f._initFields(instance)
356 return instance
357 } else {
358 var instance *Object
359 var thisVal Value
360 if !f.derived {
361 instance = f.createInstance(args, newTarget)
362 f._initFields(instance)
363 thisVal = instance
364 }
365 ret := f._call(args, newTarget, thisVal)
366
367 if ret, ok := ret.(*Object); ok {
368 return ret
369 }
370 if f.derived {
371 r := f.val.runtime
372 if ret != _undefined {
373 panic(r.NewTypeError("Derived constructors may only return object or undefined"))
374 }
375 if v := r.vm.stack[r.vm.sp+1]; v != nil { // using residual 'this' value (a bit hacky)
376 instance = r.toObject(v)
377 } else {
378 panic(r.newError(r.getReferenceError(), "Must call super constructor in derived class before returning from derived constructor"))
379 }
380 }
381 return instance
382 }
383}
384
385func (f *classFuncObject) assertConstructor() func(args []Value, newTarget *Object) *Object {
386 return f.construct

Callers 1

ConstructMethod · 0.95

Calls 7

createInstanceMethod · 0.95
_initFieldsMethod · 0.95
_callMethod · 0.80
NewTypeErrorMethod · 0.80
newErrorMethod · 0.80
getReferenceErrorMethod · 0.80
toObjectMethod · 0.65

Tested by

no test coverage detected