builtin_function.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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))
  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 *funcObject:
  32. return newStringValue(f.src)
  33. case *classFuncObject:
  34. return newStringValue(f.src)
  35. case *methodFuncObject:
  36. return newStringValue(f.src)
  37. case *arrowFuncObject:
  38. return newStringValue(f.src)
  39. case *nativeFuncObject:
  40. return newStringValue(fmt.Sprintf("function %s() { [native code] }", nilSafe(f.getStr("name", nil)).toString()))
  41. case *boundFuncObject:
  42. return newStringValue(fmt.Sprintf("function %s() { [native code] }", nilSafe(f.getStr("name", nil)).toString()))
  43. case *lazyObject:
  44. obj.self = f.create(obj)
  45. goto repeat
  46. case *proxyObject:
  47. repeat2:
  48. switch c := f.target.self.(type) {
  49. case *classFuncObject, *methodFuncObject, *funcObject, *arrowFuncObject, *nativeFuncObject, *boundFuncObject:
  50. return asciiString("function () { [native code] }")
  51. case *lazyObject:
  52. f.target.self = c.create(obj)
  53. goto repeat2
  54. }
  55. }
  56. panic(r.NewTypeError("Function.prototype.toString requires that 'this' be a Function"))
  57. }
  58. func (r *Runtime) functionproto_hasInstance(call FunctionCall) Value {
  59. if o, ok := call.This.(*Object); ok {
  60. if _, ok = o.self.assertCallable(); ok {
  61. return r.toBoolean(o.self.hasInstance(call.Argument(0)))
  62. }
  63. }
  64. return valueFalse
  65. }
  66. func (r *Runtime) createListFromArrayLike(a Value) []Value {
  67. o := r.toObject(a)
  68. if arr := r.checkStdArrayObj(o); arr != nil {
  69. return arr.values
  70. }
  71. l := toLength(o.self.getStr("length", nil))
  72. res := make([]Value, 0, l)
  73. for k := int64(0); k < l; k++ {
  74. res = append(res, nilSafe(o.self.getIdx(valueInt(k), nil)))
  75. }
  76. return res
  77. }
  78. func (r *Runtime) functionproto_apply(call FunctionCall) Value {
  79. var args []Value
  80. if len(call.Arguments) >= 2 {
  81. args = r.createListFromArrayLike(call.Arguments[1])
  82. }
  83. f := r.toCallable(call.This)
  84. return f(FunctionCall{
  85. This: call.Argument(0),
  86. Arguments: args,
  87. })
  88. }
  89. func (r *Runtime) functionproto_call(call FunctionCall) Value {
  90. var args []Value
  91. if len(call.Arguments) > 0 {
  92. args = call.Arguments[1:]
  93. }
  94. f := r.toCallable(call.This)
  95. return f(FunctionCall{
  96. This: call.Argument(0),
  97. Arguments: args,
  98. })
  99. }
  100. func (r *Runtime) boundCallable(target func(FunctionCall) Value, boundArgs []Value) func(FunctionCall) Value {
  101. var this Value
  102. var args []Value
  103. if len(boundArgs) > 0 {
  104. this = boundArgs[0]
  105. args = make([]Value, len(boundArgs)-1)
  106. copy(args, boundArgs[1:])
  107. } else {
  108. this = _undefined
  109. }
  110. return func(call FunctionCall) Value {
  111. a := append(args, call.Arguments...)
  112. return target(FunctionCall{
  113. This: this,
  114. Arguments: a,
  115. })
  116. }
  117. }
  118. func (r *Runtime) boundConstruct(f *Object, target func([]Value, *Object) *Object, boundArgs []Value) func([]Value, *Object) *Object {
  119. if target == nil {
  120. return nil
  121. }
  122. var args []Value
  123. if len(boundArgs) > 1 {
  124. args = make([]Value, len(boundArgs)-1)
  125. copy(args, boundArgs[1:])
  126. }
  127. return func(fargs []Value, newTarget *Object) *Object {
  128. a := append(args, fargs...)
  129. if newTarget == f {
  130. newTarget = nil
  131. }
  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. var l = _positiveZero
  140. if obj.self.hasOwnPropertyStr("length") {
  141. var li int64
  142. switch lenProp := nilSafe(obj.self.getStr("length", nil)).(type) {
  143. case valueInt:
  144. li = lenProp.ToInteger()
  145. case valueFloat:
  146. switch lenProp {
  147. case _positiveInf:
  148. l = lenProp
  149. goto lenNotInt
  150. case _negativeInf:
  151. goto lenNotInt
  152. case _negativeZero:
  153. // no-op, li == 0
  154. default:
  155. if !math.IsNaN(float64(lenProp)) {
  156. li = int64(math.Abs(float64(lenProp)))
  157. } // else li = 0
  158. }
  159. }
  160. if len(call.Arguments) > 1 {
  161. li -= int64(len(call.Arguments)) - 1
  162. }
  163. if li < 0 {
  164. li = 0
  165. }
  166. l = intToValue(li)
  167. }
  168. lenNotInt:
  169. name := obj.self.getStr("name", nil)
  170. nameStr := stringBound_
  171. if s, ok := name.(valueString); ok {
  172. nameStr = nameStr.concat(s)
  173. }
  174. v := &Object{runtime: r}
  175. ff := r.newNativeFuncAndConstruct(v, r.boundCallable(fcall, call.Arguments), r.boundConstruct(v, construct, call.Arguments), nil, nameStr.string(), l)
  176. bf := &boundFuncObject{
  177. nativeFuncObject: *ff,
  178. wrapped: obj,
  179. }
  180. bf.prototype = obj.self.proto()
  181. v.self = bf
  182. return v
  183. }
  184. func (r *Runtime) initFunction() {
  185. o := r.global.FunctionPrototype.self.(*nativeFuncObject)
  186. o.prototype = r.global.ObjectPrototype
  187. o._putProp("name", stringEmpty, false, false, true)
  188. o._putProp("apply", r.newNativeFunc(r.functionproto_apply, nil, "apply", nil, 2), true, false, true)
  189. o._putProp("bind", r.newNativeFunc(r.functionproto_bind, nil, "bind", nil, 1), true, false, true)
  190. o._putProp("call", r.newNativeFunc(r.functionproto_call, nil, "call", nil, 1), true, false, true)
  191. o._putProp("toString", r.newNativeFunc(r.functionproto_toString, nil, "toString", nil, 0), true, false, true)
  192. o._putSym(SymHasInstance, valueProp(r.newNativeFunc(r.functionproto_hasInstance, nil, "[Symbol.hasInstance]", nil, 1), false, false, false))
  193. r.global.Function = r.newNativeFuncConstruct(r.builtin_Function, "Function", r.global.FunctionPrototype, 1)
  194. r.addToGlobal("Function", r.global.Function)
  195. }