builtin_function.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. ret := r.toObject(r.eval(src, false, false, _undefined))
  19. ret.self.setProto(proto, true)
  20. return ret
  21. }
  22. func (r *Runtime) functionproto_toString(call FunctionCall) Value {
  23. obj := r.toObject(call.This)
  24. repeat:
  25. switch f := obj.self.(type) {
  26. case *funcObject:
  27. return newStringValue(f.src)
  28. case *nativeFuncObject:
  29. return newStringValue(fmt.Sprintf("function %s() { [native code] }", f.nameProp.get(call.This).toString()))
  30. case *boundFuncObject:
  31. return newStringValue(fmt.Sprintf("function %s() { [native code] }", f.nameProp.get(call.This).toString()))
  32. case *lazyObject:
  33. obj.self = f.create(obj)
  34. goto repeat
  35. case *proxyObject:
  36. var name string
  37. repeat2:
  38. switch c := f.target.self.(type) {
  39. case *funcObject:
  40. name = c.src
  41. case *nativeFuncObject:
  42. name = c.nameProp.get(call.This).String()
  43. case *boundFuncObject:
  44. name = c.nameProp.get(call.This).String()
  45. case *lazyObject:
  46. f.target.self = c.create(obj)
  47. goto repeat2
  48. default:
  49. name = f.target.String()
  50. }
  51. return newStringValue(fmt.Sprintf("function proxy() { [%s] }", name))
  52. }
  53. r.typeErrorResult(true, "Object is not a function")
  54. return nil
  55. }
  56. func (r *Runtime) functionproto_hasInstance(call FunctionCall) Value {
  57. if o, ok := call.This.(*Object); ok {
  58. if _, ok = o.self.assertCallable(); ok {
  59. return r.toBoolean(o.self.hasInstance(call.Argument(0)))
  60. }
  61. }
  62. return valueFalse
  63. }
  64. func (r *Runtime) createListFromArrayLike(a Value) []Value {
  65. o := r.toObject(a)
  66. if arr := r.checkStdArrayObj(o); arr != nil {
  67. return arr.values
  68. }
  69. l := toLength(o.self.getStr("length", nil))
  70. res := make([]Value, 0, l)
  71. for k := int64(0); k < l; k++ {
  72. res = append(res, o.self.getIdx(valueInt(k), nil))
  73. }
  74. return res
  75. }
  76. func (r *Runtime) functionproto_apply(call FunctionCall) Value {
  77. var args []Value
  78. if len(call.Arguments) >= 2 {
  79. args = r.createListFromArrayLike(call.Arguments[1])
  80. }
  81. f := r.toCallable(call.This)
  82. return f(FunctionCall{
  83. This: call.Argument(0),
  84. Arguments: args,
  85. })
  86. }
  87. func (r *Runtime) functionproto_call(call FunctionCall) Value {
  88. var args []Value
  89. if len(call.Arguments) > 0 {
  90. args = call.Arguments[1:]
  91. }
  92. f := r.toCallable(call.This)
  93. return f(FunctionCall{
  94. This: call.Argument(0),
  95. Arguments: args,
  96. })
  97. }
  98. func (r *Runtime) boundCallable(target func(FunctionCall) Value, boundArgs []Value) func(FunctionCall) Value {
  99. var this Value
  100. var args []Value
  101. if len(boundArgs) > 0 {
  102. this = boundArgs[0]
  103. args = make([]Value, len(boundArgs)-1)
  104. copy(args, boundArgs[1:])
  105. } else {
  106. this = _undefined
  107. }
  108. return func(call FunctionCall) Value {
  109. a := append(args, call.Arguments...)
  110. return target(FunctionCall{
  111. This: this,
  112. Arguments: a,
  113. })
  114. }
  115. }
  116. func (r *Runtime) boundConstruct(target func([]Value, *Object) *Object, boundArgs []Value) func([]Value, *Object) *Object {
  117. if target == nil {
  118. return nil
  119. }
  120. var args []Value
  121. if len(boundArgs) > 1 {
  122. args = make([]Value, len(boundArgs)-1)
  123. copy(args, boundArgs[1:])
  124. }
  125. return func(fargs []Value, newTarget *Object) *Object {
  126. a := append(args, fargs...)
  127. copy(a, args)
  128. return target(a, newTarget)
  129. }
  130. }
  131. func (r *Runtime) functionproto_bind(call FunctionCall) Value {
  132. obj := r.toObject(call.This)
  133. fcall := r.toCallable(call.This)
  134. construct := obj.self.assertConstructor()
  135. l := int(toUint32(obj.self.getStr("length", nil)))
  136. l -= len(call.Arguments) - 1
  137. if l < 0 {
  138. l = 0
  139. }
  140. name := obj.self.getStr("name", nil)
  141. nameStr := stringBound_
  142. if s, ok := name.(valueString); ok {
  143. nameStr = nameStr.concat(s)
  144. }
  145. v := &Object{runtime: r}
  146. ff := r.newNativeFuncObj(v, r.boundCallable(fcall, call.Arguments), r.boundConstruct(construct, call.Arguments), nameStr.string(), nil, l)
  147. v.self = &boundFuncObject{
  148. nativeFuncObject: *ff,
  149. wrapped: obj,
  150. }
  151. //ret := r.newNativeFunc(r.boundCallable(f, call.Arguments), nil, "", nil, l)
  152. //o := ret.self
  153. //o.putStr("caller", r.global.throwerProperty, false)
  154. //o.putStr("arguments", r.global.throwerProperty, false)
  155. return v
  156. }
  157. func (r *Runtime) initFunction() {
  158. o := r.global.FunctionPrototype.self.(*nativeFuncObject)
  159. o.prototype = r.global.ObjectPrototype
  160. o.nameProp.value = stringEmpty
  161. o._putProp("apply", r.newNativeFunc(r.functionproto_apply, nil, "apply", nil, 2), true, false, true)
  162. o._putProp("bind", r.newNativeFunc(r.functionproto_bind, nil, "bind", nil, 1), true, false, true)
  163. o._putProp("call", r.newNativeFunc(r.functionproto_call, nil, "call", nil, 1), true, false, true)
  164. o._putProp("toString", r.newNativeFunc(r.functionproto_toString, nil, "toString", nil, 0), true, false, true)
  165. o._putSym(symHasInstance, valueProp(r.newNativeFunc(r.functionproto_hasInstance, nil, "[Symbol.hasInstance]", nil, 1), false, false, false))
  166. r.global.Function = r.newNativeFuncConstruct(r.builtin_Function, "Function", r.global.FunctionPrototype, 1)
  167. r.addToGlobal("Function", r.global.Function)
  168. }