...

Source file src/github.com/dop251/goja/builtin_boolean.go

Documentation: github.com/dop251/goja

     1  package goja
     2  
     3  func (r *Runtime) booleanproto_toString(call FunctionCall) Value {
     4  	var b bool
     5  	switch o := call.This.(type) {
     6  	case valueBool:
     7  		b = bool(o)
     8  		goto success
     9  	case *Object:
    10  		if p, ok := o.self.(*primitiveValueObject); ok {
    11  			if b1, ok := p.pValue.(valueBool); ok {
    12  				b = bool(b1)
    13  				goto success
    14  			}
    15  		}
    16  		if o, ok := o.self.(*objectGoReflect); ok {
    17  			if o.class == classBoolean && o.toString != nil {
    18  				return o.toString()
    19  			}
    20  		}
    21  	}
    22  	r.typeErrorResult(true, "Method Boolean.prototype.toString is called on incompatible receiver")
    23  
    24  success:
    25  	if b {
    26  		return stringTrue
    27  	}
    28  	return stringFalse
    29  }
    30  
    31  func (r *Runtime) booleanproto_valueOf(call FunctionCall) Value {
    32  	switch o := call.This.(type) {
    33  	case valueBool:
    34  		return o
    35  	case *Object:
    36  		if p, ok := o.self.(*primitiveValueObject); ok {
    37  			if b, ok := p.pValue.(valueBool); ok {
    38  				return b
    39  			}
    40  		}
    41  		if o, ok := o.self.(*objectGoReflect); ok {
    42  			if o.class == classBoolean && o.valueOf != nil {
    43  				return o.valueOf()
    44  			}
    45  		}
    46  	}
    47  
    48  	r.typeErrorResult(true, "Method Boolean.prototype.valueOf is called on incompatible receiver")
    49  	return nil
    50  }
    51  
    52  func (r *Runtime) getBooleanPrototype() *Object {
    53  	ret := r.global.BooleanPrototype
    54  	if ret == nil {
    55  		ret = r.newPrimitiveObject(valueFalse, r.global.ObjectPrototype, classBoolean)
    56  		r.global.BooleanPrototype = ret
    57  		o := ret.self
    58  		o._putProp("toString", r.newNativeFunc(r.booleanproto_toString, "toString", 0), true, false, true)
    59  		o._putProp("valueOf", r.newNativeFunc(r.booleanproto_valueOf, "valueOf", 0), true, false, true)
    60  		o._putProp("constructor", r.getBoolean(), true, false, true)
    61  	}
    62  	return ret
    63  }
    64  
    65  func (r *Runtime) getBoolean() *Object {
    66  	ret := r.global.Boolean
    67  	if ret == nil {
    68  		ret = &Object{runtime: r}
    69  		r.global.Boolean = ret
    70  		proto := r.getBooleanPrototype()
    71  		r.newNativeFuncAndConstruct(ret, r.builtin_Boolean,
    72  			r.wrapNativeConstruct(r.builtin_newBoolean, ret, proto), proto, "Boolean", intToValue(1))
    73  	}
    74  	return ret
    75  }
    76  

View as plain text