builtin_function.go 5.3 KB

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