summaryrefslogtreecommitdiffstats
path: root/js/src/vm/CallAndConstruct.cpp
blob: be714e3dd0a9c3ba3515fff555352820550fbbe8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/* -*- Mode.h: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
 * vim: set ts=8 sts=2 et sw=2 tw=80:
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "js/CallAndConstruct.h"

#include "mozilla/Assertions.h"  // MOZ_ASSERT

#include "jstypes.h"                  // JS_PUBLIC_API
#include "gc/Zone.h"                  // js::Zone
#include "js/Context.h"               // AssertHeapIsIdle
#include "js/friend/ErrorMessages.h"  // JSMSG_*
#include "js/RootingAPI.h"    // JS::Rooted, JS::Handle, JS::MutableHandle
#include "js/Value.h"         // JS::Value, JS::*Value
#include "js/ValueArray.h"    // JS::HandleValueArray
#include "vm/BytecodeUtil.h"  // JSDVG_IGNORE_STACK
#include "vm/Interpreter.h"   // js::Call, js::Construct
#include "vm/JSAtom.h"        // JSAtom, js::Atomize
#include "vm/JSContext.h"     // JSContext, CHECK_THREAD, ReportValueError
#include "vm/JSObject.h"      // JSObject
#include "vm/Stack.h"  // js::InvokeArgs, js::FillArgumentsFromArraylike, js::ConstructArgs

#include "vm/JSContext-inl.h"         // JSContext::check
#include "vm/JSObject-inl.h"          // js::IsConstructor
#include "vm/ObjectOperations-inl.h"  // js::GetProperty

using namespace js;

JS_PUBLIC_API bool JS::IsCallable(JSObject* obj) { return obj->isCallable(); }

JS_PUBLIC_API bool JS::IsConstructor(JSObject* obj) {
  return obj->isConstructor();
}

JS_PUBLIC_API bool JS_CallFunctionValue(JSContext* cx,
                                        JS::Handle<JSObject*> obj,
                                        JS::Handle<JS::Value> fval,
                                        const JS::HandleValueArray& args,
                                        JS::MutableHandle<JS::Value> rval) {
  MOZ_ASSERT(!cx->zone()->isAtomsZone());
  AssertHeapIsIdle();
  CHECK_THREAD(cx);
  cx->check(obj, fval, args);

  js::InvokeArgs iargs(cx);
  if (!FillArgumentsFromArraylike(cx, iargs, args)) {
    return false;
  }

  JS::Rooted<JS::Value> thisv(cx, JS::ObjectOrNullValue(obj));
  return js::Call(cx, fval, thisv, iargs, rval);
}

JS_PUBLIC_API bool JS_CallFunction(JSContext* cx, JS::Handle<JSObject*> obj,
                                   JS::Handle<JSFunction*> fun,
                                   const JS::HandleValueArray& args,
                                   JS::MutableHandle<JS::Value> rval) {
  MOZ_ASSERT(!cx->zone()->isAtomsZone());
  AssertHeapIsIdle();
  CHECK_THREAD(cx);
  cx->check(obj, fun, args);

  js::InvokeArgs iargs(cx);
  if (!FillArgumentsFromArraylike(cx, iargs, args)) {
    return false;
  }

  JS::Rooted<JS::Value> fval(cx, JS::ObjectValue(*fun));
  JS::Rooted<JS::Value> thisv(cx, JS::ObjectOrNullValue(obj));
  return js::Call(cx, fval, thisv, iargs, rval);
}

JS_PUBLIC_API bool JS_CallFunctionName(JSContext* cx, JS::Handle<JSObject*> obj,
                                       const char* name,
                                       const JS::HandleValueArray& args,
                                       JS::MutableHandle<JS::Value> rval) {
  MOZ_ASSERT(!cx->zone()->isAtomsZone());
  AssertHeapIsIdle();
  CHECK_THREAD(cx);
  cx->check(obj, args);

  JSAtom* atom = Atomize(cx, name, strlen(name));
  if (!atom) {
    return false;
  }

  JS::Rooted<JS::Value> fval(cx);
  JS::Rooted<jsid> id(cx, AtomToId(atom));
  if (!GetProperty(cx, obj, obj, id, &fval)) {
    return false;
  }

  js::InvokeArgs iargs(cx);
  if (!FillArgumentsFromArraylike(cx, iargs, args)) {
    return false;
  }

  JS::Rooted<JS::Value> thisv(cx, JS::ObjectOrNullValue(obj));
  return js::Call(cx, fval, thisv, iargs, rval);
}

JS_PUBLIC_API bool JS::Call(JSContext* cx, JS::Handle<JS::Value> thisv,
                            JS::Handle<JS::Value> fval,
                            const JS::HandleValueArray& args,
                            JS::MutableHandle<JS::Value> rval) {
  AssertHeapIsIdle();
  CHECK_THREAD(cx);
  cx->check(thisv, fval, args);

  js::InvokeArgs iargs(cx);
  if (!FillArgumentsFromArraylike(cx, iargs, args)) {
    return false;
  }

  return js::Call(cx, fval, thisv, iargs, rval);
}

JS_PUBLIC_API bool JS::Construct(JSContext* cx, JS::Handle<JS::Value> fval,
                                 JS::Handle<JSObject*> newTarget,
                                 const JS::HandleValueArray& args,
                                 JS::MutableHandle<JSObject*> objp) {
  AssertHeapIsIdle();
  CHECK_THREAD(cx);
  cx->check(fval, newTarget, args);

  if (!js::IsConstructor(fval)) {
    ReportValueError(cx, JSMSG_NOT_CONSTRUCTOR, JSDVG_IGNORE_STACK, fval,
                     nullptr);
    return false;
  }

  JS::Rooted<JS::Value> newTargetVal(cx, JS::ObjectValue(*newTarget));
  if (!js::IsConstructor(newTargetVal)) {
    ReportValueError(cx, JSMSG_NOT_CONSTRUCTOR, JSDVG_IGNORE_STACK,
                     newTargetVal, nullptr);
    return false;
  }

  js::ConstructArgs cargs(cx);
  if (!FillArgumentsFromArraylike(cx, cargs, args)) {
    return false;
  }

  return js::Construct(cx, fval, cargs, newTargetVal, objp);
}

JS_PUBLIC_API bool JS::Construct(JSContext* cx, JS::Handle<JS::Value> fval,
                                 const JS::HandleValueArray& args,
                                 JS::MutableHandle<JSObject*> objp) {
  AssertHeapIsIdle();
  CHECK_THREAD(cx);
  cx->check(fval, args);

  if (!js::IsConstructor(fval)) {
    ReportValueError(cx, JSMSG_NOT_CONSTRUCTOR, JSDVG_IGNORE_STACK, fval,
                     nullptr);
    return false;
  }

  js::ConstructArgs cargs(cx);
  if (!FillArgumentsFromArraylike(cx, cargs, args)) {
    return false;
  }

  return js::Construct(cx, fval, cargs, fval, objp);
}