go.ketch.com/lib/goja@v0.0.1/builtin_boolean.go (about)

     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  	}
    17  	r.typeErrorResult(true, "Method Boolean.prototype.toString is called on incompatible receiver")
    18  
    19  success:
    20  	if b {
    21  		return stringTrue
    22  	}
    23  	return stringFalse
    24  }
    25  
    26  func (r *Runtime) booleanproto_valueOf(call FunctionCall) Value {
    27  	switch o := call.This.(type) {
    28  	case valueBool:
    29  		return o
    30  	case *Object:
    31  		if p, ok := o.self.(*primitiveValueObject); ok {
    32  			if b, ok := p.pValue.(valueBool); ok {
    33  				return b
    34  			}
    35  		}
    36  	}
    37  
    38  	r.typeErrorResult(true, "Method Boolean.prototype.valueOf is called on incompatible receiver")
    39  	return nil
    40  }
    41  
    42  func (r *Runtime) initBoolean() {
    43  	r.global.BooleanPrototype = r.newPrimitiveObject(valueFalse, r.global.ObjectPrototype, classBoolean)
    44  	o := r.global.BooleanPrototype.self
    45  	o._putProp("toString", r.newNativeFunc(r.booleanproto_toString, nil, "toString", nil, 0), true, false, true)
    46  	o._putProp("valueOf", r.newNativeFunc(r.booleanproto_valueOf, nil, "valueOf", nil, 0), true, false, true)
    47  
    48  	r.global.Boolean = r.newNativeFunc(r.builtin_Boolean, r.builtin_newBoolean, "Boolean", r.global.BooleanPrototype, 1)
    49  	r.addToGlobal("Boolean", r.global.Boolean)
    50  }