builtin_boolean.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package goja
  2. func (r *Runtime) booleanproto_toString(call FunctionCall) Value {
  3. var b bool
  4. switch o := call.This.(type) {
  5. case valueBool:
  6. b = bool(o)
  7. goto success
  8. case *Object:
  9. if p, ok := o.self.(*primitiveValueObject); ok {
  10. if b1, ok := p.pValue.(valueBool); ok {
  11. b = bool(b1)
  12. goto success
  13. }
  14. }
  15. }
  16. r.typeErrorResult(true, "Method Boolean.prototype.toString is called on incompatible receiver")
  17. success:
  18. if b {
  19. return stringTrue
  20. }
  21. return stringFalse
  22. }
  23. func (r *Runtime) booleanproto_valueOf(call FunctionCall) Value {
  24. switch o := call.This.(type) {
  25. case valueBool:
  26. return o
  27. case *Object:
  28. if p, ok := o.self.(*primitiveValueObject); ok {
  29. if b, ok := p.pValue.(valueBool); ok {
  30. return b
  31. }
  32. }
  33. }
  34. r.typeErrorResult(true, "Method Boolean.prototype.valueOf is called on incompatible receiver")
  35. return nil
  36. }
  37. func (r *Runtime) initBoolean() {
  38. r.global.BooleanPrototype = r.newPrimitiveObject(valueFalse, r.global.ObjectPrototype, classBoolean)
  39. o := r.global.BooleanPrototype.self
  40. o._putProp("toString", r.newNativeFunc(r.booleanproto_toString, nil, "toString", nil, 0), true, false, true)
  41. o._putProp("valueOf", r.newNativeFunc(r.booleanproto_valueOf, nil, "valueOf", nil, 0), true, false, true)
  42. r.global.Boolean = r.newNativeFunc(r.builtin_Boolean, r.builtin_newBoolean, "Boolean", r.global.BooleanPrototype, 1)
  43. r.addToGlobal("Boolean", r.global.Boolean)
  44. }