builtin_function.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. package goja
  2. import (
  3. "math"
  4. )
  5. func (r *Runtime) functionCtor(args []Value, proto *Object, async bool) *Object {
  6. var sb valueStringBuilder
  7. if async {
  8. sb.WriteString(asciiString("(async function anonymous("))
  9. } else {
  10. sb.WriteString(asciiString("(function anonymous("))
  11. }
  12. if len(args) > 1 {
  13. ar := args[:len(args)-1]
  14. for i, arg := range ar {
  15. sb.WriteString(arg.toString())
  16. if i < len(ar)-1 {
  17. sb.WriteRune(',')
  18. }
  19. }
  20. }
  21. sb.WriteString(asciiString("\n) {\n"))
  22. if len(args) > 0 {
  23. sb.WriteString(args[len(args)-1].toString())
  24. }
  25. sb.WriteString(asciiString("\n})"))
  26. ret := r.toObject(r.eval(sb.String(), false, false))
  27. ret.self.setProto(proto, true)
  28. return ret
  29. }
  30. func (r *Runtime) builtin_Function(args []Value, proto *Object) *Object {
  31. return r.functionCtor(args, proto, false)
  32. }
  33. func (r *Runtime) builtin_asyncFunction(args []Value, proto *Object) *Object {
  34. return r.functionCtor(args, proto, true)
  35. }
  36. func (r *Runtime) functionproto_toString(call FunctionCall) Value {
  37. obj := r.toObject(call.This)
  38. if lazy, ok := obj.self.(*lazyObject); ok {
  39. obj.self = lazy.create(obj)
  40. }
  41. switch f := obj.self.(type) {
  42. case funcObjectImpl:
  43. return f.source()
  44. case *proxyObject:
  45. if lazy, ok := f.target.self.(*lazyObject); ok {
  46. f.target.self = lazy.create(f.target)
  47. }
  48. if _, ok := f.target.self.(funcObjectImpl); ok {
  49. return asciiString("function () { [native code] }")
  50. }
  51. }
  52. panic(r.NewTypeError("Function.prototype.toString requires that 'this' be a Function"))
  53. }
  54. func (r *Runtime) functionproto_hasInstance(call FunctionCall) Value {
  55. if o, ok := call.This.(*Object); ok {
  56. if _, ok = o.self.assertCallable(); ok {
  57. return r.toBoolean(o.self.hasInstance(call.Argument(0)))
  58. }
  59. }
  60. return valueFalse
  61. }
  62. func (r *Runtime) createListFromArrayLike(a Value) []Value {
  63. o := r.toObject(a)
  64. if arr := r.checkStdArrayObj(o); arr != nil {
  65. return arr.values
  66. }
  67. l := toLength(o.self.getStr("length", nil))
  68. res := make([]Value, 0, l)
  69. for k := int64(0); k < l; k++ {
  70. res = append(res, nilSafe(o.self.getIdx(valueInt(k), nil)))
  71. }
  72. return res
  73. }
  74. func (r *Runtime) functionproto_apply(call FunctionCall) Value {
  75. var args []Value
  76. if len(call.Arguments) >= 2 {
  77. args = r.createListFromArrayLike(call.Arguments[1])
  78. }
  79. f := r.toCallable(call.This)
  80. return f(FunctionCall{
  81. This: call.Argument(0),
  82. Arguments: args,
  83. })
  84. }
  85. func (r *Runtime) functionproto_call(call FunctionCall) Value {
  86. var args []Value
  87. if len(call.Arguments) > 0 {
  88. args = call.Arguments[1:]
  89. }
  90. f := r.toCallable(call.This)
  91. return f(FunctionCall{
  92. This: call.Argument(0),
  93. Arguments: args,
  94. })
  95. }
  96. func (r *Runtime) boundCallable(target func(FunctionCall) Value, boundArgs []Value) func(FunctionCall) Value {
  97. var this Value
  98. var args []Value
  99. if len(boundArgs) > 0 {
  100. this = boundArgs[0]
  101. args = make([]Value, len(boundArgs)-1)
  102. copy(args, boundArgs[1:])
  103. } else {
  104. this = _undefined
  105. }
  106. return func(call FunctionCall) Value {
  107. a := append(args, call.Arguments...)
  108. return target(FunctionCall{
  109. This: this,
  110. Arguments: a,
  111. })
  112. }
  113. }
  114. func (r *Runtime) boundConstruct(f *Object, target func([]Value, *Object) *Object, boundArgs []Value) func([]Value, *Object) *Object {
  115. if target == nil {
  116. return nil
  117. }
  118. var args []Value
  119. if len(boundArgs) > 1 {
  120. args = make([]Value, len(boundArgs)-1)
  121. copy(args, boundArgs[1:])
  122. }
  123. return func(fargs []Value, newTarget *Object) *Object {
  124. a := append(args, fargs...)
  125. if newTarget == f {
  126. newTarget = nil
  127. }
  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.newNativeFuncAndConstruct(v, r.boundCallable(fcall, call.Arguments), r.boundConstruct(v, construct, call.Arguments), nil, nameStr.string(), l)
  172. bf := &boundFuncObject{
  173. nativeFuncObject: *ff,
  174. wrapped: obj,
  175. }
  176. bf.prototype = obj.self.proto()
  177. v.self = bf
  178. return v
  179. }
  180. func (r *Runtime) initFunction() {
  181. o := r.global.FunctionPrototype.self.(*nativeFuncObject)
  182. o.prototype = r.global.ObjectPrototype
  183. o._putProp("name", stringEmpty, false, false, true)
  184. o._putProp("apply", r.newNativeFunc(r.functionproto_apply, nil, "apply", nil, 2), true, false, true)
  185. o._putProp("bind", r.newNativeFunc(r.functionproto_bind, nil, "bind", nil, 1), true, false, true)
  186. o._putProp("call", r.newNativeFunc(r.functionproto_call, nil, "call", nil, 1), true, false, true)
  187. o._putProp("toString", r.newNativeFunc(r.functionproto_toString, nil, "toString", nil, 0), true, false, true)
  188. o._putSym(SymHasInstance, valueProp(r.newNativeFunc(r.functionproto_hasInstance, nil, "[Symbol.hasInstance]", nil, 1), false, false, false))
  189. r.global.Function = r.newNativeFuncConstruct(r.builtin_Function, "Function", r.global.FunctionPrototype, 1)
  190. r.addToGlobal("Function", r.global.Function)
  191. }
  192. func (r *Runtime) createAsyncFunctionProto(val *Object) objectImpl {
  193. o := &baseObject{
  194. class: classObject,
  195. val: val,
  196. extensible: true,
  197. prototype: r.global.FunctionPrototype,
  198. }
  199. o.init()
  200. o._putProp("constructor", r.global.AsyncFunction, true, false, true)
  201. o._putSym(SymToStringTag, valueProp(asciiString(classAsyncFunction), false, false, true))
  202. return o
  203. }
  204. func (r *Runtime) createAsyncFunction(val *Object) objectImpl {
  205. o := r.newNativeFuncConstructObj(val, r.builtin_asyncFunction, "AsyncFunction", r.global.AsyncFunctionPrototype, 1)
  206. return o
  207. }
  208. func (r *Runtime) initAsyncFunction() {
  209. r.global.AsyncFunctionPrototype = r.newLazyObject(r.createAsyncFunctionProto)
  210. r.global.AsyncFunction = r.newLazyObject(r.createAsyncFunction)
  211. }