builtin_function.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. package goja
  2. import (
  3. "fmt"
  4. "math"
  5. )
  6. func (r *Runtime) builtin_Function(args []Value, proto *Object) *Object {
  7. var sb valueStringBuilder
  8. sb.WriteString(asciiString("(function anonymous("))
  9. if len(args) > 1 {
  10. ar := args[:len(args)-1]
  11. for i, arg := range ar {
  12. sb.WriteString(arg.toString())
  13. if i < len(ar)-1 {
  14. sb.WriteRune(',')
  15. }
  16. }
  17. }
  18. sb.WriteString(asciiString("\n) {\n"))
  19. if len(args) > 0 {
  20. sb.WriteString(args[len(args)-1].toString())
  21. }
  22. sb.WriteString(asciiString("\n})"))
  23. ret := r.toObject(r.eval(sb.String(), false, false, _undefined))
  24. ret.self.setProto(proto, true)
  25. return ret
  26. }
  27. func (r *Runtime) functionproto_toString(call FunctionCall) Value {
  28. obj := r.toObject(call.This)
  29. repeat:
  30. switch f := obj.self.(type) {
  31. case *methodFuncObject:
  32. return newStringValue(f.src)
  33. case *funcObject:
  34. return newStringValue(f.src)
  35. case *arrowFuncObject:
  36. return newStringValue(f.src)
  37. case *nativeFuncObject:
  38. return newStringValue(fmt.Sprintf("function %s() { [native code] }", nilSafe(f.getStr("name", nil)).toString()))
  39. case *boundFuncObject:
  40. return newStringValue(fmt.Sprintf("function %s() { [native code] }", nilSafe(f.getStr("name", nil)).toString()))
  41. case *lazyObject:
  42. obj.self = f.create(obj)
  43. goto repeat
  44. case *proxyObject:
  45. repeat2:
  46. switch c := f.target.self.(type) {
  47. case *methodFuncObject, *funcObject, *arrowFuncObject, *nativeFuncObject, *boundFuncObject:
  48. return asciiString("function () { [native code] }")
  49. case *lazyObject:
  50. f.target.self = c.create(obj)
  51. goto repeat2
  52. }
  53. }
  54. panic(r.NewTypeError("Function.prototype.toString requires that 'this' be a Function"))
  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, nilSafe(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. var l = _positiveZero
  136. if obj.self.hasOwnPropertyStr("length") {
  137. var li int64
  138. switch lenProp := nilSafe(obj.self.getStr("length", nil)).(type) {
  139. case valueInt:
  140. li = lenProp.ToInteger()
  141. case valueFloat:
  142. switch lenProp {
  143. case _positiveInf:
  144. l = lenProp
  145. goto lenNotInt
  146. case _negativeInf:
  147. goto lenNotInt
  148. case _negativeZero:
  149. // no-op, li == 0
  150. default:
  151. if !math.IsNaN(float64(lenProp)) {
  152. li = int64(math.Abs(float64(lenProp)))
  153. } // else li = 0
  154. }
  155. }
  156. if len(call.Arguments) > 1 {
  157. li -= int64(len(call.Arguments)) - 1
  158. }
  159. if li < 0 {
  160. li = 0
  161. }
  162. l = intToValue(li)
  163. }
  164. lenNotInt:
  165. name := obj.self.getStr("name", nil)
  166. nameStr := stringBound_
  167. if s, ok := name.(valueString); ok {
  168. nameStr = nameStr.concat(s)
  169. }
  170. v := &Object{runtime: r}
  171. ff := r.newNativeFuncObj(v, r.boundCallable(fcall, call.Arguments), r.boundConstruct(construct, call.Arguments), nameStr.string(), nil, l)
  172. v.self = &boundFuncObject{
  173. nativeFuncObject: *ff,
  174. wrapped: obj,
  175. }
  176. return v
  177. }
  178. func (r *Runtime) initFunction() {
  179. o := r.global.FunctionPrototype.self.(*nativeFuncObject)
  180. o.prototype = r.global.ObjectPrototype
  181. o._putProp("name", stringEmpty, false, false, true)
  182. o._putProp("apply", r.newNativeFunc(r.functionproto_apply, nil, "apply", nil, 2), true, false, true)
  183. o._putProp("bind", r.newNativeFunc(r.functionproto_bind, nil, "bind", nil, 1), true, false, true)
  184. o._putProp("call", r.newNativeFunc(r.functionproto_call, nil, "call", nil, 1), true, false, true)
  185. o._putProp("toString", r.newNativeFunc(r.functionproto_toString, nil, "toString", nil, 0), true, false, true)
  186. o._putSym(SymHasInstance, valueProp(r.newNativeFunc(r.functionproto_hasInstance, nil, "[Symbol.hasInstance]", nil, 1), false, false, false))
  187. r.global.Function = r.newNativeFuncConstruct(r.builtin_Function, "Function", r.global.FunctionPrototype, 1)
  188. r.addToGlobal("Function", r.global.Function)
  189. }