builtin_function.go 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. package goja
  2. import (
  3. "math"
  4. )
  5. func (r *Runtime) functionCtor(args []Value, proto *Object, async, generator bool) *Object {
  6. var sb StringBuilder
  7. if async {
  8. if generator {
  9. sb.WriteString(asciiString("(async function* anonymous("))
  10. } else {
  11. sb.WriteString(asciiString("(async function anonymous("))
  12. }
  13. } else {
  14. if generator {
  15. sb.WriteString(asciiString("(function* anonymous("))
  16. } else {
  17. sb.WriteString(asciiString("(function anonymous("))
  18. }
  19. }
  20. if len(args) > 1 {
  21. ar := args[:len(args)-1]
  22. for i, arg := range ar {
  23. sb.WriteString(arg.toString())
  24. if i < len(ar)-1 {
  25. sb.WriteRune(',')
  26. }
  27. }
  28. }
  29. sb.WriteString(asciiString("\n) {\n"))
  30. if len(args) > 0 {
  31. sb.WriteString(args[len(args)-1].toString())
  32. }
  33. sb.WriteString(asciiString("\n})"))
  34. ret := r.toObject(r.eval(sb.String(), false, false))
  35. ret.self.setProto(proto, true)
  36. return ret
  37. }
  38. func (r *Runtime) builtin_Function(args []Value, proto *Object) *Object {
  39. return r.functionCtor(args, proto, false, false)
  40. }
  41. func (r *Runtime) builtin_asyncFunction(args []Value, proto *Object) *Object {
  42. return r.functionCtor(args, proto, true, false)
  43. }
  44. func (r *Runtime) builtin_generatorFunction(args []Value, proto *Object) *Object {
  45. return r.functionCtor(args, proto, false, true)
  46. }
  47. func (r *Runtime) functionproto_toString(call FunctionCall) Value {
  48. obj := r.toObject(call.This)
  49. if lazy, ok := obj.self.(*lazyObject); ok {
  50. obj.self = lazy.create(obj)
  51. }
  52. switch f := obj.self.(type) {
  53. case funcObjectImpl:
  54. return f.source()
  55. case *proxyObject:
  56. if lazy, ok := f.target.self.(*lazyObject); ok {
  57. f.target.self = lazy.create(f.target)
  58. }
  59. if _, ok := f.target.self.(funcObjectImpl); ok {
  60. return asciiString("function () { [native code] }")
  61. }
  62. }
  63. panic(r.NewTypeError("Function.prototype.toString requires that 'this' be a Function"))
  64. }
  65. func (r *Runtime) functionproto_hasInstance(call FunctionCall) Value {
  66. if o, ok := call.This.(*Object); ok {
  67. if _, ok = o.self.assertCallable(); ok {
  68. return r.toBoolean(o.self.hasInstance(call.Argument(0)))
  69. }
  70. }
  71. return valueFalse
  72. }
  73. func (r *Runtime) createListFromArrayLike(a Value) []Value {
  74. o := r.toObject(a)
  75. if arr := r.checkStdArrayObj(o); arr != nil {
  76. return arr.values
  77. }
  78. l := toLength(o.self.getStr("length", nil))
  79. res := make([]Value, 0, l)
  80. for k := int64(0); k < l; k++ {
  81. res = append(res, nilSafe(o.self.getIdx(valueInt(k), nil)))
  82. }
  83. return res
  84. }
  85. func (r *Runtime) functionproto_apply(call FunctionCall) Value {
  86. var args []Value
  87. if len(call.Arguments) >= 2 {
  88. args = r.createListFromArrayLike(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) functionproto_call(call FunctionCall) Value {
  97. var args []Value
  98. if len(call.Arguments) > 0 {
  99. args = call.Arguments[1:]
  100. }
  101. f := r.toCallable(call.This)
  102. return f(FunctionCall{
  103. This: call.Argument(0),
  104. Arguments: args,
  105. })
  106. }
  107. func (r *Runtime) boundCallable(target func(FunctionCall) Value, boundArgs []Value) func(FunctionCall) Value {
  108. var this Value
  109. var args []Value
  110. if len(boundArgs) > 0 {
  111. this = boundArgs[0]
  112. args = make([]Value, len(boundArgs)-1)
  113. copy(args, boundArgs[1:])
  114. } else {
  115. this = _undefined
  116. }
  117. return func(call FunctionCall) Value {
  118. a := append(args, call.Arguments...)
  119. return target(FunctionCall{
  120. This: this,
  121. Arguments: a,
  122. })
  123. }
  124. }
  125. func (r *Runtime) boundConstruct(f *Object, target func([]Value, *Object) *Object, boundArgs []Value) func([]Value, *Object) *Object {
  126. if target == nil {
  127. return nil
  128. }
  129. var args []Value
  130. if len(boundArgs) > 1 {
  131. args = make([]Value, len(boundArgs)-1)
  132. copy(args, boundArgs[1:])
  133. }
  134. return func(fargs []Value, newTarget *Object) *Object {
  135. a := append(args, fargs...)
  136. if newTarget == f {
  137. newTarget = nil
  138. }
  139. return target(a, newTarget)
  140. }
  141. }
  142. func (r *Runtime) functionproto_bind(call FunctionCall) Value {
  143. obj := r.toObject(call.This)
  144. fcall := r.toCallable(call.This)
  145. construct := obj.self.assertConstructor()
  146. var l = _positiveZero
  147. if obj.self.hasOwnPropertyStr("length") {
  148. var li int64
  149. switch lenProp := nilSafe(obj.self.getStr("length", nil)).(type) {
  150. case valueInt:
  151. li = lenProp.ToInteger()
  152. case valueFloat:
  153. switch lenProp {
  154. case _positiveInf:
  155. l = lenProp
  156. goto lenNotInt
  157. case _negativeInf:
  158. goto lenNotInt
  159. case _negativeZero:
  160. // no-op, li == 0
  161. default:
  162. if !math.IsNaN(float64(lenProp)) {
  163. li = int64(math.Abs(float64(lenProp)))
  164. } // else li = 0
  165. }
  166. }
  167. if len(call.Arguments) > 1 {
  168. li -= int64(len(call.Arguments)) - 1
  169. }
  170. if li < 0 {
  171. li = 0
  172. }
  173. l = intToValue(li)
  174. }
  175. lenNotInt:
  176. name := obj.self.getStr("name", nil)
  177. nameStr := stringBound_
  178. if s, ok := name.(String); ok {
  179. nameStr = nameStr.Concat(s)
  180. }
  181. v := &Object{runtime: r}
  182. ff := r.newNativeFuncAndConstruct(v, r.boundCallable(fcall, call.Arguments), r.boundConstruct(v, construct, call.Arguments), nil, nameStr.string(), l)
  183. bf := &boundFuncObject{
  184. nativeFuncObject: *ff,
  185. wrapped: obj,
  186. }
  187. bf.prototype = obj.self.proto()
  188. v.self = bf
  189. return v
  190. }
  191. func (r *Runtime) initFunction() {
  192. o := r.global.FunctionPrototype.self.(*nativeFuncObject)
  193. o.prototype = r.global.ObjectPrototype
  194. o._putProp("name", stringEmpty, false, false, true)
  195. o._putProp("apply", r.newNativeFunc(r.functionproto_apply, nil, "apply", nil, 2), true, false, true)
  196. o._putProp("bind", r.newNativeFunc(r.functionproto_bind, nil, "bind", nil, 1), true, false, true)
  197. o._putProp("call", r.newNativeFunc(r.functionproto_call, nil, "call", nil, 1), true, false, true)
  198. o._putProp("toString", r.newNativeFunc(r.functionproto_toString, nil, "toString", nil, 0), true, false, true)
  199. o._putSym(SymHasInstance, valueProp(r.newNativeFunc(r.functionproto_hasInstance, nil, "[Symbol.hasInstance]", nil, 1), false, false, false))
  200. r.global.Function = r.newNativeFuncConstruct(r.builtin_Function, "Function", r.global.FunctionPrototype, 1)
  201. r.addToGlobal("Function", r.global.Function)
  202. }
  203. func (r *Runtime) createAsyncFunctionProto(val *Object) objectImpl {
  204. o := &baseObject{
  205. class: classObject,
  206. val: val,
  207. extensible: true,
  208. prototype: r.global.FunctionPrototype,
  209. }
  210. o.init()
  211. o._putProp("constructor", r.getAsyncFunction(), true, false, true)
  212. o._putSym(SymToStringTag, valueProp(asciiString(classAsyncFunction), false, false, true))
  213. return o
  214. }
  215. func (r *Runtime) getAsyncFunctionPrototype() *Object {
  216. var o *Object
  217. if o = r.global.AsyncFunctionPrototype; o == nil {
  218. o = r.newLazyObject(r.createAsyncFunctionProto)
  219. r.global.AsyncFunctionPrototype = o
  220. }
  221. return o
  222. }
  223. func (r *Runtime) createAsyncFunction(val *Object) objectImpl {
  224. o := r.newNativeFuncConstructObj(val, r.builtin_asyncFunction, "AsyncFunction", r.getAsyncFunctionPrototype(), 1)
  225. return o
  226. }
  227. func (r *Runtime) getAsyncFunction() *Object {
  228. var o *Object
  229. if o = r.global.AsyncFunction; o == nil {
  230. o = &Object{runtime: r}
  231. r.global.AsyncFunction = o
  232. o.self = r.createAsyncFunction(o)
  233. }
  234. return o
  235. }
  236. func (r *Runtime) builtin_genproto_next(call FunctionCall) Value {
  237. if o, ok := call.This.(*Object); ok {
  238. if gen, ok := o.self.(*generatorObject); ok {
  239. return gen.next(call.Argument(0))
  240. }
  241. }
  242. panic(r.NewTypeError("Method [Generator].prototype.next called on incompatible receiver"))
  243. }
  244. func (r *Runtime) builtin_genproto_return(call FunctionCall) Value {
  245. if o, ok := call.This.(*Object); ok {
  246. if gen, ok := o.self.(*generatorObject); ok {
  247. return gen._return(call.Argument(0))
  248. }
  249. }
  250. panic(r.NewTypeError("Method [Generator].prototype.return called on incompatible receiver"))
  251. }
  252. func (r *Runtime) builtin_genproto_throw(call FunctionCall) Value {
  253. if o, ok := call.This.(*Object); ok {
  254. if gen, ok := o.self.(*generatorObject); ok {
  255. return gen.throw(call.Argument(0))
  256. }
  257. }
  258. panic(r.NewTypeError("Method [Generator].prototype.throw called on incompatible receiver"))
  259. }
  260. func (r *Runtime) createGeneratorFunctionProto(val *Object) objectImpl {
  261. o := newBaseObjectObj(val, r.global.FunctionPrototype, classObject)
  262. o._putProp("constructor", r.getGeneratorFunction(), false, false, true)
  263. o._putProp("prototype", r.getGeneratorPrototype(), false, false, true)
  264. o._putSym(SymToStringTag, valueProp(asciiString(classGeneratorFunction), false, false, true))
  265. return o
  266. }
  267. func (r *Runtime) getGeneratorFunctionPrototype() *Object {
  268. var o *Object
  269. if o = r.global.GeneratorFunctionPrototype; o == nil {
  270. o = r.newLazyObject(r.createGeneratorFunctionProto)
  271. r.global.GeneratorFunctionPrototype = o
  272. }
  273. return o
  274. }
  275. func (r *Runtime) createGeneratorFunction(val *Object) objectImpl {
  276. o := r.newNativeFuncConstructObj(val, r.builtin_generatorFunction, "GeneratorFunction", r.getGeneratorFunctionPrototype(), 1)
  277. return o
  278. }
  279. func (r *Runtime) getGeneratorFunction() *Object {
  280. var o *Object
  281. if o = r.global.GeneratorFunction; o == nil {
  282. o = &Object{runtime: r}
  283. r.global.GeneratorFunction = o
  284. o.self = r.createGeneratorFunction(o)
  285. }
  286. return o
  287. }
  288. func (r *Runtime) createGeneratorProto(val *Object) objectImpl {
  289. o := newBaseObjectObj(val, r.getIteratorPrototype(), classObject)
  290. o._putProp("constructor", r.getGeneratorFunctionPrototype(), false, false, true)
  291. o._putProp("next", r.newNativeFunc(r.builtin_genproto_next, nil, "next", nil, 1), true, false, true)
  292. o._putProp("return", r.newNativeFunc(r.builtin_genproto_return, nil, "return", nil, 1), true, false, true)
  293. o._putProp("throw", r.newNativeFunc(r.builtin_genproto_throw, nil, "throw", nil, 1), true, false, true)
  294. o._putSym(SymToStringTag, valueProp(asciiString(classGenerator), false, false, true))
  295. return o
  296. }
  297. func (r *Runtime) getGeneratorPrototype() *Object {
  298. var o *Object
  299. if o = r.global.GeneratorPrototype; o == nil {
  300. o = &Object{runtime: r}
  301. r.global.GeneratorPrototype = o
  302. o.self = r.createGeneratorProto(o)
  303. }
  304. return o
  305. }