builtin_function.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. package goja
  2. import (
  3. "fmt"
  4. )
  5. func (r *Runtime) builtin_Function(args []Value, proto *Object) *Object {
  6. src := "(function anonymous("
  7. if len(args) > 1 {
  8. for _, arg := range args[:len(args)-1] {
  9. src += arg.String() + ","
  10. }
  11. src = src[:len(src)-1]
  12. }
  13. body := ""
  14. if len(args) > 0 {
  15. body = args[len(args)-1].String()
  16. }
  17. src += "){" + body + "})"
  18. return r.toObject(r.eval(src, false, false, _undefined))
  19. }
  20. func (r *Runtime) functionproto_toString(call FunctionCall) Value {
  21. obj := r.toObject(call.This)
  22. repeat:
  23. switch f := obj.self.(type) {
  24. case *funcObject:
  25. return newStringValue(f.src)
  26. case *nativeFuncObject:
  27. return newStringValue(fmt.Sprintf("function %s() { [native code] }", f.nameProp.get(call.This).ToString()))
  28. case *boundFuncObject:
  29. return newStringValue(fmt.Sprintf("function %s() { [native code] }", f.nameProp.get(call.This).ToString()))
  30. case *lazyObject:
  31. obj.self = f.create(obj)
  32. goto repeat
  33. }
  34. r.typeErrorResult(true, "Object is not a function")
  35. return nil
  36. }
  37. func (r *Runtime) toValueArray(a Value) []Value {
  38. obj := r.toObject(a)
  39. l := toUInt32(obj.self.getStr("length"))
  40. ret := make([]Value, l)
  41. for i := uint32(0); i < l; i++ {
  42. o := obj.self.get(valueInt(i))
  43. if o == nil {
  44. o = _undefined
  45. }
  46. ret[i] = o
  47. }
  48. return ret
  49. }
  50. func (r *Runtime) functionproto_apply(call FunctionCall) Value {
  51. f := r.toCallable(call.This)
  52. var args []Value
  53. if len(call.Arguments) >= 2 {
  54. args = r.toValueArray(call.Arguments[1])
  55. }
  56. return f(FunctionCall{
  57. This: call.Argument(0),
  58. Arguments: args,
  59. })
  60. }
  61. func (r *Runtime) functionproto_call(call FunctionCall) Value {
  62. f := r.toCallable(call.This)
  63. var args []Value
  64. if len(call.Arguments) > 0 {
  65. args = call.Arguments[1:]
  66. }
  67. return f(FunctionCall{
  68. This: call.Argument(0),
  69. Arguments: args,
  70. })
  71. }
  72. func (r *Runtime) boundCallable(target func(FunctionCall) Value, boundArgs []Value) func(FunctionCall) Value {
  73. var this Value
  74. var args []Value
  75. if len(boundArgs) > 0 {
  76. this = boundArgs[0]
  77. args = make([]Value, len(boundArgs)-1)
  78. copy(args, boundArgs[1:])
  79. } else {
  80. this = _undefined
  81. }
  82. return func(call FunctionCall) Value {
  83. a := append(args, call.Arguments...)
  84. return target(FunctionCall{
  85. This: this,
  86. Arguments: a,
  87. })
  88. }
  89. }
  90. func (r *Runtime) boundConstruct(target func([]Value) *Object, boundArgs []Value) func([]Value) *Object {
  91. if target == nil {
  92. return nil
  93. }
  94. var args []Value
  95. if len(boundArgs) > 1 {
  96. args = make([]Value, len(boundArgs)-1)
  97. copy(args, boundArgs[1:])
  98. }
  99. return func(fargs []Value) *Object {
  100. a := append(args, fargs...)
  101. copy(a, args)
  102. return target(a)
  103. }
  104. }
  105. func (r *Runtime) functionproto_bind(call FunctionCall) Value {
  106. obj := r.toObject(call.This)
  107. f := obj.self
  108. var fcall func(FunctionCall) Value
  109. var construct func([]Value) *Object
  110. repeat:
  111. switch ff := f.(type) {
  112. case *funcObject:
  113. fcall = ff.Call
  114. construct = ff.construct
  115. case *nativeFuncObject:
  116. fcall = ff.f
  117. construct = ff.construct
  118. case *boundFuncObject:
  119. f = &ff.nativeFuncObject
  120. goto repeat
  121. case *lazyObject:
  122. f = ff.create(obj)
  123. goto repeat
  124. default:
  125. r.typeErrorResult(true, "Value is not callable: %s", obj.ToString())
  126. }
  127. l := int(toUInt32(obj.self.getStr("length")))
  128. l -= len(call.Arguments) - 1
  129. if l < 0 {
  130. l = 0
  131. }
  132. v := &Object{runtime: r}
  133. ff := r.newNativeFuncObj(v, r.boundCallable(fcall, call.Arguments), r.boundConstruct(construct, call.Arguments), "", nil, l)
  134. v.self = &boundFuncObject{
  135. nativeFuncObject: *ff,
  136. }
  137. //ret := r.newNativeFunc(r.boundCallable(f, call.Arguments), nil, "", nil, l)
  138. //o := ret.self
  139. //o.putStr("caller", r.global.throwerProperty, false)
  140. //o.putStr("arguments", r.global.throwerProperty, false)
  141. return v
  142. }
  143. func (r *Runtime) initFunction() {
  144. o := r.global.FunctionPrototype.self
  145. o.(*nativeFuncObject).prototype = r.global.ObjectPrototype
  146. o._putProp("toString", r.newNativeFunc(r.functionproto_toString, nil, "toString", nil, 0), true, false, true)
  147. o._putProp("apply", r.newNativeFunc(r.functionproto_apply, nil, "apply", nil, 2), true, false, true)
  148. o._putProp("call", r.newNativeFunc(r.functionproto_call, nil, "call", nil, 1), true, false, true)
  149. o._putProp("bind", r.newNativeFunc(r.functionproto_bind, nil, "bind", nil, 1), true, false, true)
  150. r.global.Function = r.newNativeFuncConstruct(r.builtin_Function, "Function", r.global.FunctionPrototype, 1)
  151. r.addToGlobal("Function", r.global.Function)
  152. }