blob: f757d1dd69969acbf17db4abc1fcdf4fd87ff8c7 [file] [log] [blame]
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001// Copyright 2012 the V8 project authors. All rights reserved.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
Steve Blocka7e24c12009-10-30 11:49:00 +00004
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005#include "src/api.h"
Ben Murdochf87a2032010-10-22 12:50:53 +01006
Ben Murdoch3ef787d2012-04-12 10:51:47 +01007#include <string.h> // For memcpy, strlen.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008#ifdef V8_USE_ADDRESS_SANITIZER
9#include <sanitizer/asan_interface.h>
10#endif // V8_USE_ADDRESS_SANITIZER
11#include <cmath> // For isnan.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000012#include <limits>
13#include <vector>
Ben Murdochb8a8cc12014-11-26 15:28:44 +000014#include "include/v8-debug.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000015#include "include/v8-experimental.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000016#include "include/v8-profiler.h"
17#include "include/v8-testing.h"
Ben Murdoch097c5b22016-05-18 11:27:45 +010018#include "src/accessors.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000019#include "src/api-experimental.h"
20#include "src/api-natives.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000021#include "src/assert-scope.h"
22#include "src/background-parsing-task.h"
Emily Bernierd0a1eb72015-03-24 16:35:39 -040023#include "src/base/functional.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000024#include "src/base/platform/platform.h"
25#include "src/base/platform/time.h"
26#include "src/base/utils/random-number-generator.h"
27#include "src/bootstrapper.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000028#include "src/char-predicates-inl.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000029#include "src/code-stubs.h"
30#include "src/compiler.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000031#include "src/context-measure.h"
32#include "src/contexts.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000033#include "src/conversions-inl.h"
34#include "src/counters.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000035#include "src/debug/debug.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000036#include "src/deoptimizer.h"
37#include "src/execution.h"
Ben Murdoch097c5b22016-05-18 11:27:45 +010038#include "src/gdb-jit.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000039#include "src/global-handles.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000040#include "src/icu_util.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000041#include "src/isolate-inl.h"
Ben Murdoch097c5b22016-05-18 11:27:45 +010042#include "src/json-parser.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000043#include "src/messages.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000044#include "src/parsing/parser.h"
45#include "src/parsing/scanner-character-streams.h"
46#include "src/pending-compilation-error-handler.h"
47#include "src/profiler/cpu-profiler.h"
48#include "src/profiler/heap-profiler.h"
49#include "src/profiler/heap-snapshot-generator-inl.h"
50#include "src/profiler/profile-generator-inl.h"
Ben Murdochc5610432016-08-08 18:44:38 +010051#include "src/profiler/tick-sample.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000052#include "src/property-descriptor.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000053#include "src/property-details.h"
Ben Murdochc5610432016-08-08 18:44:38 +010054#include "src/property.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000055#include "src/prototype.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000056#include "src/runtime-profiler.h"
Ben Murdochc5610432016-08-08 18:44:38 +010057#include "src/runtime/runtime.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000058#include "src/simulator.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000059#include "src/snapshot/natives.h"
60#include "src/snapshot/snapshot.h"
61#include "src/startup-data-util.h"
Ben Murdoch097c5b22016-05-18 11:27:45 +010062#include "src/tracing/trace-event.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000063#include "src/unicode-inl.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000064#include "src/v8.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000065#include "src/v8threads.h"
66#include "src/version.h"
67#include "src/vm-state-inl.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000068
Steve Blocka7e24c12009-10-30 11:49:00 +000069namespace v8 {
70
Ben Murdochc5610432016-08-08 18:44:38 +010071#define LOG_API(isolate, class_name, function_name) \
72 i::RuntimeCallTimerScope _runtime_timer( \
73 isolate, &i::RuntimeCallStats::API_##class_name##_##function_name); \
74 LOG(isolate, ApiEntryCall("v8::" #class_name "::" #function_name))
Steve Blocka7e24c12009-10-30 11:49:00 +000075
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000076#define ENTER_V8(isolate) i::VMState<v8::OTHER> __state__((isolate))
77
Ben Murdochc5610432016-08-08 18:44:38 +010078#define PREPARE_FOR_EXECUTION_GENERIC(isolate, context, class_name, \
79 function_name, bailout_value, \
80 HandleScopeClass, do_callback) \
81 if (IsExecutionTerminatingCheck(isolate)) { \
82 return bailout_value; \
83 } \
84 HandleScopeClass handle_scope(isolate); \
85 CallDepthScope call_depth_scope(isolate, context, do_callback); \
86 LOG_API(isolate, class_name, function_name); \
87 ENTER_V8(isolate); \
Steve Blocka7e24c12009-10-30 11:49:00 +000088 bool has_pending_exception = false
89
Ben Murdochc5610432016-08-08 18:44:38 +010090#define PREPARE_FOR_EXECUTION_WITH_CONTEXT(context, class_name, function_name, \
91 bailout_value, HandleScopeClass, \
92 do_callback) \
93 auto isolate = context.IsEmpty() \
94 ? i::Isolate::Current() \
95 : reinterpret_cast<i::Isolate*>(context->GetIsolate()); \
96 PREPARE_FOR_EXECUTION_GENERIC(isolate, context, class_name, function_name, \
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000097 bailout_value, HandleScopeClass, do_callback);
98
Ben Murdochc5610432016-08-08 18:44:38 +010099#define PREPARE_FOR_EXECUTION_WITH_ISOLATE(isolate, class_name, function_name, \
100 T) \
101 PREPARE_FOR_EXECUTION_GENERIC(isolate, Local<Context>(), class_name, \
102 function_name, MaybeLocal<T>(), \
103 InternalEscapableScope, false);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000104
Ben Murdochc5610432016-08-08 18:44:38 +0100105#define PREPARE_FOR_EXECUTION(context, class_name, function_name, T) \
106 PREPARE_FOR_EXECUTION_WITH_CONTEXT(context, class_name, function_name, \
107 MaybeLocal<T>(), InternalEscapableScope, \
108 false)
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000109
Ben Murdochc5610432016-08-08 18:44:38 +0100110#define PREPARE_FOR_EXECUTION_WITH_CALLBACK(context, class_name, \
111 function_name, T) \
112 PREPARE_FOR_EXECUTION_WITH_CONTEXT(context, class_name, function_name, \
113 MaybeLocal<T>(), InternalEscapableScope, \
114 true)
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000115
Ben Murdochc5610432016-08-08 18:44:38 +0100116#define PREPARE_FOR_EXECUTION_PRIMITIVE(context, class_name, function_name, T) \
117 PREPARE_FOR_EXECUTION_WITH_CONTEXT(context, class_name, function_name, \
118 Nothing<T>(), i::HandleScope, false)
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000119
120#define EXCEPTION_BAILOUT_CHECK_SCOPED(isolate, value) \
121 do { \
122 if (has_pending_exception) { \
123 call_depth_scope.Escape(); \
124 return value; \
125 } \
Steve Blocka7e24c12009-10-30 11:49:00 +0000126 } while (false)
127
128
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000129#define RETURN_ON_FAILED_EXECUTION(T) \
130 EXCEPTION_BAILOUT_CHECK_SCOPED(isolate, MaybeLocal<T>())
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100131
132
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000133#define RETURN_ON_FAILED_EXECUTION_PRIMITIVE(T) \
134 EXCEPTION_BAILOUT_CHECK_SCOPED(isolate, Nothing<T>())
135
136
137#define RETURN_TO_LOCAL_UNCHECKED(maybe_local, T) \
138 return maybe_local.FromMaybe(Local<T>());
139
140
141#define RETURN_ESCAPED(value) return handle_scope.Escape(value);
142
143
144namespace {
145
146Local<Context> ContextFromHeapObject(i::Handle<i::Object> obj) {
147 return reinterpret_cast<v8::Isolate*>(i::HeapObject::cast(*obj)->GetIsolate())
148 ->GetCurrentContext();
149}
150
151class InternalEscapableScope : public v8::EscapableHandleScope {
152 public:
153 explicit inline InternalEscapableScope(i::Isolate* isolate)
154 : v8::EscapableHandleScope(reinterpret_cast<v8::Isolate*>(isolate)) {}
155};
156
157
Ben Murdochda12d292016-06-02 14:46:10 +0100158#ifdef DEBUG
159void CheckMicrotasksScopesConsistency(i::Isolate* isolate) {
160 auto handle_scope_implementer = isolate->handle_scope_implementer();
161 if (handle_scope_implementer->microtasks_policy() ==
162 v8::MicrotasksPolicy::kScoped) {
163 DCHECK(handle_scope_implementer->GetMicrotasksScopeDepth() ||
164 !handle_scope_implementer->DebugMicrotasksScopeDepthIsZero());
165 }
166}
167#endif
168
169
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000170class CallDepthScope {
171 public:
172 explicit CallDepthScope(i::Isolate* isolate, Local<Context> context,
173 bool do_callback)
174 : isolate_(isolate),
175 context_(context),
176 escaped_(false),
177 do_callback_(do_callback) {
178 // TODO(dcarney): remove this when blink stops crashing.
179 DCHECK(!isolate_->external_caught_exception());
180 isolate_->IncrementJsCallsFromApiCounter();
181 isolate_->handle_scope_implementer()->IncrementCallDepth();
182 if (!context_.IsEmpty()) context_->Enter();
Ben Murdoch097c5b22016-05-18 11:27:45 +0100183 if (do_callback_) isolate_->FireBeforeCallEnteredCallback();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000184 }
185 ~CallDepthScope() {
186 if (!context_.IsEmpty()) context_->Exit();
187 if (!escaped_) isolate_->handle_scope_implementer()->DecrementCallDepth();
188 if (do_callback_) isolate_->FireCallCompletedCallback();
Ben Murdochda12d292016-06-02 14:46:10 +0100189#ifdef DEBUG
190 if (do_callback_) CheckMicrotasksScopesConsistency(isolate_);
191#endif
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000192 }
193
194 void Escape() {
195 DCHECK(!escaped_);
196 escaped_ = true;
197 auto handle_scope_implementer = isolate_->handle_scope_implementer();
198 handle_scope_implementer->DecrementCallDepth();
199 bool call_depth_is_zero = handle_scope_implementer->CallDepthIsZero();
200 isolate_->OptionalRescheduleException(call_depth_is_zero);
201 }
202
203 private:
204 i::Isolate* const isolate_;
205 Local<Context> context_;
206 bool escaped_;
207 bool do_callback_;
208};
209
210} // namespace
211
212
213static ScriptOrigin GetScriptOriginForScript(i::Isolate* isolate,
214 i::Handle<i::Script> script) {
215 i::Handle<i::Object> scriptName(i::Script::GetNameOrSourceURL(script));
216 i::Handle<i::Object> source_map_url(script->source_mapping_url(), isolate);
217 v8::Isolate* v8_isolate =
218 reinterpret_cast<v8::Isolate*>(script->GetIsolate());
219 ScriptOriginOptions options(script->origin_options());
220 v8::ScriptOrigin origin(
221 Utils::ToLocal(scriptName),
222 v8::Integer::New(v8_isolate, script->line_offset()),
223 v8::Integer::New(v8_isolate, script->column_offset()),
224 v8::Boolean::New(v8_isolate, options.IsSharedCrossOrigin()),
225 v8::Integer::New(v8_isolate, script->id()),
226 v8::Boolean::New(v8_isolate, options.IsEmbedderDebugScript()),
227 Utils::ToLocal(source_map_url),
228 v8::Boolean::New(v8_isolate, options.IsOpaque()));
229 return origin;
230}
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100231
232
Steve Blocka7e24c12009-10-30 11:49:00 +0000233// --- E x c e p t i o n B e h a v i o r ---
234
235
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800236void i::FatalProcessOutOfMemory(const char* location) {
237 i::V8::FatalProcessOutOfMemory(location, false);
238}
239
Steve Blocka7e24c12009-10-30 11:49:00 +0000240
241// When V8 cannot allocated memory FatalProcessOutOfMemory is called.
242// The default fatal error handler is called and execution is stopped.
Ben Murdochc5610432016-08-08 18:44:38 +0100243void i::V8::FatalProcessOutOfMemory(const char* location, bool is_heap_oom) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000244 i::Isolate* isolate = i::Isolate::Current();
245 char last_few_messages[Heap::kTraceRingBufferSize + 1];
246 char js_stacktrace[Heap::kStacktraceBufferSize + 1];
247 memset(last_few_messages, 0, Heap::kTraceRingBufferSize + 1);
248 memset(js_stacktrace, 0, Heap::kStacktraceBufferSize + 1);
249
Steve Blockd0582a62009-12-15 09:54:21 +0000250 i::HeapStats heap_stats;
251 int start_marker;
252 heap_stats.start_marker = &start_marker;
253 int new_space_size;
254 heap_stats.new_space_size = &new_space_size;
255 int new_space_capacity;
256 heap_stats.new_space_capacity = &new_space_capacity;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000257 intptr_t old_space_size;
258 heap_stats.old_space_size = &old_space_size;
259 intptr_t old_space_capacity;
260 heap_stats.old_space_capacity = &old_space_capacity;
Ben Murdochf87a2032010-10-22 12:50:53 +0100261 intptr_t code_space_size;
Steve Blockd0582a62009-12-15 09:54:21 +0000262 heap_stats.code_space_size = &code_space_size;
Ben Murdochf87a2032010-10-22 12:50:53 +0100263 intptr_t code_space_capacity;
Steve Blockd0582a62009-12-15 09:54:21 +0000264 heap_stats.code_space_capacity = &code_space_capacity;
Ben Murdochf87a2032010-10-22 12:50:53 +0100265 intptr_t map_space_size;
Steve Blockd0582a62009-12-15 09:54:21 +0000266 heap_stats.map_space_size = &map_space_size;
Ben Murdochf87a2032010-10-22 12:50:53 +0100267 intptr_t map_space_capacity;
Steve Blockd0582a62009-12-15 09:54:21 +0000268 heap_stats.map_space_capacity = &map_space_capacity;
Ben Murdochf87a2032010-10-22 12:50:53 +0100269 intptr_t lo_space_size;
Steve Blockd0582a62009-12-15 09:54:21 +0000270 heap_stats.lo_space_size = &lo_space_size;
271 int global_handle_count;
272 heap_stats.global_handle_count = &global_handle_count;
273 int weak_global_handle_count;
274 heap_stats.weak_global_handle_count = &weak_global_handle_count;
275 int pending_global_handle_count;
276 heap_stats.pending_global_handle_count = &pending_global_handle_count;
277 int near_death_global_handle_count;
278 heap_stats.near_death_global_handle_count = &near_death_global_handle_count;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000279 int free_global_handle_count;
280 heap_stats.free_global_handle_count = &free_global_handle_count;
Ben Murdochf87a2032010-10-22 12:50:53 +0100281 intptr_t memory_allocator_size;
Ben Murdochbb769b22010-08-11 14:56:33 +0100282 heap_stats.memory_allocator_size = &memory_allocator_size;
Ben Murdochf87a2032010-10-22 12:50:53 +0100283 intptr_t memory_allocator_capacity;
Ben Murdochbb769b22010-08-11 14:56:33 +0100284 heap_stats.memory_allocator_capacity = &memory_allocator_capacity;
285 int objects_per_type[LAST_TYPE + 1] = {0};
286 heap_stats.objects_per_type = objects_per_type;
287 int size_per_type[LAST_TYPE + 1] = {0};
288 heap_stats.size_per_type = size_per_type;
Iain Merrick75681382010-08-19 15:07:18 +0100289 int os_error;
290 heap_stats.os_error = &os_error;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000291 heap_stats.last_few_messages = last_few_messages;
292 heap_stats.js_stacktrace = js_stacktrace;
Steve Blockd0582a62009-12-15 09:54:21 +0000293 int end_marker;
294 heap_stats.end_marker = &end_marker;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000295 if (isolate->heap()->HasBeenSetUp()) {
296 // BUG(1718): Don't use the take_snapshot since we don't support
297 // HeapIterator here without doing a special GC.
298 isolate->heap()->RecordStats(&heap_stats, false);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000299 char* first_newline = strchr(last_few_messages, '\n');
300 if (first_newline == NULL || first_newline[1] == '\0')
301 first_newline = last_few_messages;
302 PrintF("\n<--- Last few GCs --->\n%s\n", first_newline);
303 PrintF("\n<--- JS stacktrace --->\n%s\n", js_stacktrace);
Steve Blocka7e24c12009-10-30 11:49:00 +0000304 }
Ben Murdochc5610432016-08-08 18:44:38 +0100305 Utils::ApiCheck(false, location, is_heap_oom
306 ? "Allocation failed - JavaScript heap out of memory"
307 : "Allocation failed - process out of memory");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000308 // If the fatal error handler returns, we stop execution.
309 FATAL("API fatal error handler returned after process out of memory");
Steve Blocka7e24c12009-10-30 11:49:00 +0000310}
311
312
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000313void Utils::ReportApiFailure(const char* location, const char* message) {
314 i::Isolate* isolate = i::Isolate::Current();
315 FatalErrorCallback callback = isolate->exception_behavior();
316 if (callback == NULL) {
317 base::OS::PrintError("\n#\n# Fatal error in %s\n# %s\n#\n\n", location,
318 message);
319 base::OS::Abort();
320 } else {
321 callback(location, message);
322 }
323 isolate->SignalFatalError();
Steve Blocka7e24c12009-10-30 11:49:00 +0000324}
325
326
Steve Block44f0eee2011-05-26 01:26:41 +0100327static inline bool IsExecutionTerminatingCheck(i::Isolate* isolate) {
Steve Block44f0eee2011-05-26 01:26:41 +0100328 if (isolate->has_scheduled_exception()) {
329 return isolate->scheduled_exception() ==
330 isolate->heap()->termination_exception();
331 }
332 return false;
333}
334
335
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000336void V8::SetNativesDataBlob(StartupData* natives_blob) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400337 i::V8::SetNativesBlob(natives_blob);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000338}
339
340
341void V8::SetSnapshotDataBlob(StartupData* snapshot_blob) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400342 i::V8::SetSnapshotBlob(snapshot_blob);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000343}
344
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000345namespace {
346
347class ArrayBufferAllocator : public v8::ArrayBuffer::Allocator {
348 public:
349 virtual void* Allocate(size_t length) {
350 void* data = AllocateUninitialized(length);
351 return data == NULL ? data : memset(data, 0, length);
352 }
353 virtual void* AllocateUninitialized(size_t length) { return malloc(length); }
354 virtual void Free(void* data, size_t) { free(data); }
355};
356
Ben Murdochda12d292016-06-02 14:46:10 +0100357bool RunExtraCode(Isolate* isolate, Local<Context> context,
358 const char* utf8_source, const char* name) {
359 base::ElapsedTimer timer;
360 timer.Start();
361 Context::Scope context_scope(context);
362 TryCatch try_catch(isolate);
363 Local<String> source_string;
364 if (!String::NewFromUtf8(isolate, utf8_source, NewStringType::kNormal)
365 .ToLocal(&source_string)) {
366 return false;
367 }
368 Local<String> resource_name =
369 String::NewFromUtf8(isolate, name, NewStringType::kNormal)
370 .ToLocalChecked();
371 ScriptOrigin origin(resource_name);
372 ScriptCompiler::Source source(source_string, origin);
373 Local<Script> script;
374 if (!ScriptCompiler::Compile(context, &source).ToLocal(&script)) return false;
375 if (script->Run(context).IsEmpty()) return false;
376 if (i::FLAG_profile_deserialization) {
377 i::PrintF("Executing custom snapshot script %s took %0.3f ms\n", name,
378 timer.Elapsed().InMillisecondsF());
379 }
380 timer.Stop();
381 CHECK(!try_catch.HasCaught());
382 return true;
383}
384
385StartupData SerializeIsolateAndContext(
386 Isolate* isolate, Persistent<Context>* context,
387 i::Snapshot::Metadata metadata,
388 i::StartupSerializer::FunctionCodeHandling function_code_handling) {
389 if (context->IsEmpty()) return {NULL, 0};
390
391 i::Isolate* internal_isolate = reinterpret_cast<i::Isolate*>(isolate);
392
393 // If we don't do this then we end up with a stray root pointing at the
394 // context even after we have disposed of the context.
395 internal_isolate->heap()->CollectAllAvailableGarbage("mksnapshot");
396
397 // GC may have cleared weak cells, so compact any WeakFixedArrays
398 // found on the heap.
399 i::HeapIterator iterator(internal_isolate->heap(),
400 i::HeapIterator::kFilterUnreachable);
401 for (i::HeapObject* o = iterator.next(); o != NULL; o = iterator.next()) {
402 if (o->IsPrototypeInfo()) {
403 i::Object* prototype_users = i::PrototypeInfo::cast(o)->prototype_users();
404 if (prototype_users->IsWeakFixedArray()) {
405 i::WeakFixedArray* array = i::WeakFixedArray::cast(prototype_users);
406 array->Compact<i::JSObject::PrototypeRegistryCompactionCallback>();
407 }
408 } else if (o->IsScript()) {
409 i::Object* shared_list = i::Script::cast(o)->shared_function_infos();
410 if (shared_list->IsWeakFixedArray()) {
411 i::WeakFixedArray* array = i::WeakFixedArray::cast(shared_list);
412 array->Compact<i::WeakFixedArray::NullCallback>();
413 }
414 }
415 }
416
417 i::Object* raw_context = *v8::Utils::OpenPersistent(*context);
418 context->Reset();
419
420 i::SnapshotByteSink snapshot_sink;
421 i::StartupSerializer ser(internal_isolate, &snapshot_sink,
422 function_code_handling);
423 ser.SerializeStrongReferences();
424
425 i::SnapshotByteSink context_sink;
426 i::PartialSerializer context_ser(internal_isolate, &ser, &context_sink);
427 context_ser.Serialize(&raw_context);
428 ser.SerializeWeakReferencesAndDeferred();
429
430 return i::Snapshot::CreateSnapshotBlob(ser, context_ser, metadata);
431}
432
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000433} // namespace
434
Ben Murdochda12d292016-06-02 14:46:10 +0100435StartupData V8::CreateSnapshotDataBlob(const char* embedded_source) {
436 // Create a new isolate and a new context from scratch, optionally run
437 // a script to embed, and serialize to create a snapshot blob.
438 StartupData result = {NULL, 0};
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000439
Ben Murdochda12d292016-06-02 14:46:10 +0100440 base::ElapsedTimer timer;
441 timer.Start();
442
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000443 ArrayBufferAllocator allocator;
Ben Murdochda12d292016-06-02 14:46:10 +0100444 i::Isolate* internal_isolate = new i::Isolate(true);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000445 internal_isolate->set_array_buffer_allocator(&allocator);
446 Isolate* isolate = reinterpret_cast<Isolate*>(internal_isolate);
Ben Murdochda12d292016-06-02 14:46:10 +0100447
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400448 {
449 Isolate::Scope isolate_scope(isolate);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000450 internal_isolate->Init(NULL);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400451 Persistent<Context> context;
452 {
453 HandleScope handle_scope(isolate);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000454 Local<Context> new_context = Context::New(isolate);
455 context.Reset(isolate, new_context);
Ben Murdochda12d292016-06-02 14:46:10 +0100456 if (embedded_source != NULL &&
457 !RunExtraCode(isolate, new_context, embedded_source, "<embedded>")) {
458 context.Reset();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000459 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400460 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000461
Ben Murdochda12d292016-06-02 14:46:10 +0100462 i::Snapshot::Metadata metadata;
463 metadata.set_embeds_script(embedded_source != NULL);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000464
Ben Murdochda12d292016-06-02 14:46:10 +0100465 result = SerializeIsolateAndContext(
466 isolate, &context, metadata, i::StartupSerializer::CLEAR_FUNCTION_CODE);
467 DCHECK(context.IsEmpty());
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400468 }
469 isolate->Dispose();
Ben Murdochda12d292016-06-02 14:46:10 +0100470
471 if (i::FLAG_profile_deserialization) {
472 i::PrintF("Creating snapshot took %0.3f ms\n",
473 timer.Elapsed().InMillisecondsF());
474 }
475 timer.Stop();
476 return result;
477}
478
479StartupData V8::WarmUpSnapshotDataBlob(StartupData cold_snapshot_blob,
480 const char* warmup_source) {
481 CHECK(cold_snapshot_blob.raw_size > 0 && cold_snapshot_blob.data != NULL);
482 CHECK(warmup_source != NULL);
483 // Use following steps to create a warmed up snapshot blob from a cold one:
484 // - Create a new isolate from the cold snapshot.
485 // - Create a new context to run the warmup script. This will trigger
486 // compilation of executed functions.
487 // - Create a new context. This context will be unpolluted.
488 // - Serialize the isolate and the second context into a new snapshot blob.
489 StartupData result = {NULL, 0};
490
491 base::ElapsedTimer timer;
492 timer.Start();
493
494 ArrayBufferAllocator allocator;
495 i::Isolate* internal_isolate = new i::Isolate(true);
496 internal_isolate->set_array_buffer_allocator(&allocator);
497 internal_isolate->set_snapshot_blob(&cold_snapshot_blob);
498 Isolate* isolate = reinterpret_cast<Isolate*>(internal_isolate);
499
500 {
501 Isolate::Scope isolate_scope(isolate);
502 i::Snapshot::Initialize(internal_isolate);
503 Persistent<Context> context;
504 bool success;
505 {
506 HandleScope handle_scope(isolate);
507 Local<Context> new_context = Context::New(isolate);
508 success = RunExtraCode(isolate, new_context, warmup_source, "<warm-up>");
509 }
510 if (success) {
511 HandleScope handle_scope(isolate);
512 isolate->ContextDisposedNotification(false);
513 Local<Context> new_context = Context::New(isolate);
514 context.Reset(isolate, new_context);
515 }
516
517 i::Snapshot::Metadata metadata;
518 metadata.set_embeds_script(i::Snapshot::EmbedsScript(internal_isolate));
519
520 result = SerializeIsolateAndContext(
521 isolate, &context, metadata, i::StartupSerializer::KEEP_FUNCTION_CODE);
522 DCHECK(context.IsEmpty());
523 }
524 isolate->Dispose();
525
526 if (i::FLAG_profile_deserialization) {
527 i::PrintF("Warming up snapshot took %0.3f ms\n",
528 timer.Elapsed().InMillisecondsF());
529 }
530 timer.Stop();
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400531 return result;
Ben Murdoch257744e2011-11-30 15:57:28 +0000532}
533
534
Steve Blocka7e24c12009-10-30 11:49:00 +0000535void V8::SetFlagsFromString(const char* str, int length) {
536 i::FlagList::SetFlagsFromString(str, length);
537}
538
539
540void V8::SetFlagsFromCommandLine(int* argc, char** argv, bool remove_flags) {
541 i::FlagList::SetFlagsFromCommandLine(argc, argv, remove_flags);
542}
543
544
Steve Blocka7e24c12009-10-30 11:49:00 +0000545RegisteredExtension* RegisteredExtension::first_extension_ = NULL;
546
547
548RegisteredExtension::RegisteredExtension(Extension* extension)
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100549 : extension_(extension) { }
Steve Blocka7e24c12009-10-30 11:49:00 +0000550
551
552void RegisteredExtension::Register(RegisteredExtension* that) {
Steve Block44f0eee2011-05-26 01:26:41 +0100553 that->next_ = first_extension_;
554 first_extension_ = that;
Steve Blocka7e24c12009-10-30 11:49:00 +0000555}
556
557
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000558void RegisteredExtension::UnregisterAll() {
559 RegisteredExtension* re = first_extension_;
560 while (re != NULL) {
561 RegisteredExtension* next = re->next();
562 delete re;
563 re = next;
564 }
565 first_extension_ = NULL;
566}
567
568
Steve Blocka7e24c12009-10-30 11:49:00 +0000569void RegisterExtension(Extension* that) {
570 RegisteredExtension* extension = new RegisteredExtension(that);
571 RegisteredExtension::Register(extension);
572}
573
574
575Extension::Extension(const char* name,
576 const char* source,
577 int dep_count,
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100578 const char** deps,
579 int source_length)
Steve Blocka7e24c12009-10-30 11:49:00 +0000580 : name_(name),
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100581 source_length_(source_length >= 0 ?
582 source_length :
583 (source ? static_cast<int>(strlen(source)) : 0)),
584 source_(source, source_length_),
Steve Blocka7e24c12009-10-30 11:49:00 +0000585 dep_count_(dep_count),
586 deps_(deps),
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000587 auto_enable_(false) {
588 CHECK(source != NULL || source_length_ == 0);
Steve Blocka7e24c12009-10-30 11:49:00 +0000589}
590
591
592ResourceConstraints::ResourceConstraints()
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000593 : max_semi_space_size_(0),
594 max_old_space_size_(0),
595 max_executable_size_(0),
596 stack_limit_(NULL),
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000597 code_range_size_(0) { }
Steve Blocka7e24c12009-10-30 11:49:00 +0000598
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000599void ResourceConstraints::ConfigureDefaults(uint64_t physical_memory,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000600 uint64_t virtual_memory_limit) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000601#if V8_OS_ANDROID
602 // Android has higher physical memory requirements before raising the maximum
603 // heap size limits since it has no swap space.
604 const uint64_t low_limit = 512ul * i::MB;
605 const uint64_t medium_limit = 1ul * i::GB;
606 const uint64_t high_limit = 2ul * i::GB;
607#else
608 const uint64_t low_limit = 512ul * i::MB;
609 const uint64_t medium_limit = 768ul * i::MB;
610 const uint64_t high_limit = 1ul * i::GB;
611#endif
Steve Blocka7e24c12009-10-30 11:49:00 +0000612
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000613 if (physical_memory <= low_limit) {
614 set_max_semi_space_size(i::Heap::kMaxSemiSpaceSizeLowMemoryDevice);
615 set_max_old_space_size(i::Heap::kMaxOldSpaceSizeLowMemoryDevice);
616 set_max_executable_size(i::Heap::kMaxExecutableSizeLowMemoryDevice);
617 } else if (physical_memory <= medium_limit) {
618 set_max_semi_space_size(i::Heap::kMaxSemiSpaceSizeMediumMemoryDevice);
619 set_max_old_space_size(i::Heap::kMaxOldSpaceSizeMediumMemoryDevice);
620 set_max_executable_size(i::Heap::kMaxExecutableSizeMediumMemoryDevice);
621 } else if (physical_memory <= high_limit) {
622 set_max_semi_space_size(i::Heap::kMaxSemiSpaceSizeHighMemoryDevice);
623 set_max_old_space_size(i::Heap::kMaxOldSpaceSizeHighMemoryDevice);
624 set_max_executable_size(i::Heap::kMaxExecutableSizeHighMemoryDevice);
625 } else {
626 set_max_semi_space_size(i::Heap::kMaxSemiSpaceSizeHugeMemoryDevice);
627 set_max_old_space_size(i::Heap::kMaxOldSpaceSizeHugeMemoryDevice);
628 set_max_executable_size(i::Heap::kMaxExecutableSizeHugeMemoryDevice);
Steve Blocka7e24c12009-10-30 11:49:00 +0000629 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000630
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000631 if (virtual_memory_limit > 0 && i::kRequiresCodeRange) {
632 // Reserve no more than 1/8 of the memory for the code range, but at most
633 // kMaximalCodeRangeSize.
634 set_code_range_size(
635 i::Min(i::kMaximalCodeRangeSize / i::MB,
636 static_cast<size_t>((virtual_memory_limit >> 3) / i::MB)));
Steve Blocka7e24c12009-10-30 11:49:00 +0000637 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000638}
639
640
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000641void SetResourceConstraints(i::Isolate* isolate,
642 const ResourceConstraints& constraints) {
643 int semi_space_size = constraints.max_semi_space_size();
644 int old_space_size = constraints.max_old_space_size();
645 int max_executable_size = constraints.max_executable_size();
646 size_t code_range_size = constraints.code_range_size();
647 if (semi_space_size != 0 || old_space_size != 0 ||
648 max_executable_size != 0 || code_range_size != 0) {
649 isolate->heap()->ConfigureHeap(semi_space_size, old_space_size,
650 max_executable_size, code_range_size);
651 }
652 if (constraints.stack_limit() != NULL) {
653 uintptr_t limit = reinterpret_cast<uintptr_t>(constraints.stack_limit());
654 isolate->stack_guard()->SetStackLimit(limit);
655 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000656}
657
658
659i::Object** V8::GlobalizeReference(i::Isolate* isolate, i::Object** obj) {
Ben Murdochc5610432016-08-08 18:44:38 +0100660 LOG_API(isolate, Persistent, New);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000661 i::Handle<i::Object> result = isolate->global_handles()->Create(*obj);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400662#ifdef VERIFY_HEAP
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000663 if (i::FLAG_verify_heap) {
664 (*obj)->ObjectVerify();
665 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400666#endif // VERIFY_HEAP
Steve Blocka7e24c12009-10-30 11:49:00 +0000667 return result.location();
668}
669
670
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000671i::Object** V8::CopyPersistent(i::Object** obj) {
672 i::Handle<i::Object> result = i::GlobalHandles::CopyGlobal(obj);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400673#ifdef VERIFY_HEAP
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000674 if (i::FLAG_verify_heap) {
675 (*obj)->ObjectVerify();
676 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400677#endif // VERIFY_HEAP
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000678 return result.location();
Steve Blocka7e24c12009-10-30 11:49:00 +0000679}
680
Ben Murdochda12d292016-06-02 14:46:10 +0100681void V8::RegisterExternallyReferencedObject(i::Object** object,
682 i::Isolate* isolate) {
683 isolate->heap()->RegisterExternallyReferencedObject(object);
684}
Steve Blocka7e24c12009-10-30 11:49:00 +0000685
Ben Murdochc5610432016-08-08 18:44:38 +0100686void V8::MakeWeak(i::Object** location, void* parameter,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000687 int internal_field_index1, int internal_field_index2,
688 WeakCallbackInfo<void>::Callback weak_callback) {
689 WeakCallbackType type = WeakCallbackType::kParameter;
690 if (internal_field_index1 == 0) {
691 if (internal_field_index2 == 1) {
692 type = WeakCallbackType::kInternalFields;
693 } else {
694 DCHECK_EQ(internal_field_index2, -1);
695 type = WeakCallbackType::kInternalFields;
696 }
697 } else {
698 DCHECK_EQ(internal_field_index1, -1);
699 DCHECK_EQ(internal_field_index2, -1);
700 }
Ben Murdochc5610432016-08-08 18:44:38 +0100701 i::GlobalHandles::MakeWeak(location, parameter, weak_callback, type);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400702}
703
Ben Murdochc5610432016-08-08 18:44:38 +0100704void V8::MakeWeak(i::Object** location, void* parameter,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000705 WeakCallbackInfo<void>::Callback weak_callback,
706 WeakCallbackType type) {
Ben Murdochc5610432016-08-08 18:44:38 +0100707 i::GlobalHandles::MakeWeak(location, parameter, weak_callback, type);
Steve Blocka7e24c12009-10-30 11:49:00 +0000708}
709
Ben Murdochc5610432016-08-08 18:44:38 +0100710void V8::MakeWeak(i::Object*** location_addr) {
711 i::GlobalHandles::MakeWeak(location_addr);
Steve Blocka7e24c12009-10-30 11:49:00 +0000712}
713
Ben Murdochc5610432016-08-08 18:44:38 +0100714void* V8::ClearWeak(i::Object** location) {
715 return i::GlobalHandles::ClearWeakness(location);
716}
Steve Blocka7e24c12009-10-30 11:49:00 +0000717
Ben Murdochc5610432016-08-08 18:44:38 +0100718void V8::DisposeGlobal(i::Object** location) {
719 i::GlobalHandles::Destroy(location);
Steve Blocka7e24c12009-10-30 11:49:00 +0000720}
721
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000722
723void V8::Eternalize(Isolate* v8_isolate, Value* value, int* index) {
724 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(v8_isolate);
725 i::Object* object = *Utils::OpenHandle(value);
726 isolate->eternal_handles()->Create(isolate, object, index);
727}
728
729
730Local<Value> V8::GetEternal(Isolate* v8_isolate, int index) {
731 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(v8_isolate);
732 return Utils::ToLocal(isolate->eternal_handles()->Get(index));
733}
734
735
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000736void V8::FromJustIsNothing() {
737 Utils::ApiCheck(false, "v8::FromJust", "Maybe value is Nothing.");
738}
739
740
741void V8::ToLocalEmpty() {
742 Utils::ApiCheck(false, "v8::ToLocalChecked", "Empty MaybeLocal.");
743}
744
745
746void V8::InternalFieldOutOfBounds(int index) {
747 Utils::ApiCheck(0 <= index && index < kInternalFieldsInWeakCallback,
748 "WeakCallbackInfo::GetInternalField",
749 "Internal field out of bounds.");
750}
751
752
Steve Blocka7e24c12009-10-30 11:49:00 +0000753// --- H a n d l e s ---
754
755
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000756HandleScope::HandleScope(Isolate* isolate) {
757 Initialize(isolate);
758}
759
760
761void HandleScope::Initialize(Isolate* isolate) {
762 i::Isolate* internal_isolate = reinterpret_cast<i::Isolate*>(isolate);
763 // We do not want to check the correct usage of the Locker class all over the
764 // place, so we do it only here: Without a HandleScope, an embedder can do
765 // almost nothing, so it is enough to check in this central place.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000766 // We make an exception if the serializer is enabled, which means that the
767 // Isolate is exclusively used to create a snapshot.
768 Utils::ApiCheck(
769 !v8::Locker::IsActive() ||
770 internal_isolate->thread_manager()->IsLockedByCurrentThread() ||
771 internal_isolate->serializer_enabled(),
772 "HandleScope::HandleScope",
773 "Entering the V8 API without proper locking in place");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000774 i::HandleScopeData* current = internal_isolate->handle_scope_data();
775 isolate_ = internal_isolate;
Steve Block44f0eee2011-05-26 01:26:41 +0100776 prev_next_ = current->next;
777 prev_limit_ = current->limit;
Steve Block44f0eee2011-05-26 01:26:41 +0100778 current->level++;
Steve Blocka7e24c12009-10-30 11:49:00 +0000779}
780
781
782HandleScope::~HandleScope() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000783 i::HandleScope::CloseScope(isolate_, prev_next_, prev_limit_);
784}
785
786
787int HandleScope::NumberOfHandles(Isolate* isolate) {
788 return i::HandleScope::NumberOfHandles(
789 reinterpret_cast<i::Isolate*>(isolate));
790}
791
792
793i::Object** HandleScope::CreateHandle(i::Isolate* isolate, i::Object* value) {
794 return i::HandleScope::CreateHandle(isolate, value);
795}
796
797
798i::Object** HandleScope::CreateHandle(i::HeapObject* heap_object,
799 i::Object* value) {
800 DCHECK(heap_object->IsHeapObject());
801 return i::HandleScope::CreateHandle(heap_object->GetIsolate(), value);
802}
803
804
805EscapableHandleScope::EscapableHandleScope(Isolate* v8_isolate) {
806 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(v8_isolate);
807 escape_slot_ = CreateHandle(isolate, isolate->heap()->the_hole_value());
808 Initialize(v8_isolate);
809}
810
811
812i::Object** EscapableHandleScope::Escape(i::Object** escape_value) {
813 i::Heap* heap = reinterpret_cast<i::Isolate*>(GetIsolate())->heap();
814 Utils::ApiCheck(*escape_slot_ == heap->the_hole_value(),
815 "EscapeableHandleScope::Escape",
816 "Escape value set twice");
817 if (escape_value == NULL) {
818 *escape_slot_ = heap->undefined_value();
819 return NULL;
Steve Blocka7e24c12009-10-30 11:49:00 +0000820 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000821 *escape_slot_ = *escape_value;
822 return escape_slot_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000823}
824
825
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000826SealHandleScope::SealHandleScope(Isolate* isolate) {
827 i::Isolate* internal_isolate = reinterpret_cast<i::Isolate*>(isolate);
828
829 isolate_ = internal_isolate;
830 i::HandleScopeData* current = internal_isolate->handle_scope_data();
831 prev_limit_ = current->limit;
832 current->limit = current->next;
833 prev_sealed_level_ = current->sealed_level;
834 current->sealed_level = current->level;
835}
836
837
838SealHandleScope::~SealHandleScope() {
839 i::HandleScopeData* current = isolate_->handle_scope_data();
840 DCHECK_EQ(current->next, current->limit);
841 current->limit = prev_limit_;
842 DCHECK_EQ(current->level, current->sealed_level);
843 current->sealed_level = prev_sealed_level_;
844}
845
846
Steve Blocka7e24c12009-10-30 11:49:00 +0000847void Context::Enter() {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000848 i::Handle<i::Context> env = Utils::OpenHandle(this);
849 i::Isolate* isolate = env->GetIsolate();
Steve Block44f0eee2011-05-26 01:26:41 +0100850 ENTER_V8(isolate);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000851 i::HandleScopeImplementer* impl = isolate->handle_scope_implementer();
852 impl->EnterContext(env);
853 impl->SaveContext(isolate->context());
Steve Block44f0eee2011-05-26 01:26:41 +0100854 isolate->set_context(*env);
Steve Blocka7e24c12009-10-30 11:49:00 +0000855}
856
857
858void Context::Exit() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000859 i::Handle<i::Context> env = Utils::OpenHandle(this);
860 i::Isolate* isolate = env->GetIsolate();
861 ENTER_V8(isolate);
862 i::HandleScopeImplementer* impl = isolate->handle_scope_implementer();
863 if (!Utils::ApiCheck(impl->LastEnteredContextWas(env),
864 "v8::Context::Exit()",
865 "Cannot exit non-entered context")) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000866 return;
867 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000868 impl->LeaveContext();
869 isolate->set_context(impl->RestoreContext());
Steve Blocka7e24c12009-10-30 11:49:00 +0000870}
871
872
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000873static void* DecodeSmiToAligned(i::Object* value, const char* location) {
874 Utils::ApiCheck(value->IsSmi(), location, "Not a Smi");
875 return reinterpret_cast<void*>(value);
Steve Blocka7e24c12009-10-30 11:49:00 +0000876}
877
878
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000879static i::Smi* EncodeAlignedAsSmi(void* value, const char* location) {
880 i::Smi* smi = reinterpret_cast<i::Smi*>(value);
881 Utils::ApiCheck(smi->IsSmi(), location, "Pointer is not aligned");
882 return smi;
883}
884
885
886static i::Handle<i::FixedArray> EmbedderDataFor(Context* context,
887 int index,
888 bool can_grow,
889 const char* location) {
890 i::Handle<i::Context> env = Utils::OpenHandle(context);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000891 i::Isolate* isolate = env->GetIsolate();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000892 bool ok =
893 Utils::ApiCheck(env->IsNativeContext(),
894 location,
895 "Not a native context") &&
896 Utils::ApiCheck(index >= 0, location, "Negative index");
897 if (!ok) return i::Handle<i::FixedArray>();
898 i::Handle<i::FixedArray> data(env->embedder_data());
899 if (index < data->length()) return data;
900 if (!Utils::ApiCheck(can_grow, location, "Index too large")) {
901 return i::Handle<i::FixedArray>();
Steve Block44f0eee2011-05-26 01:26:41 +0100902 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000903 int new_size = i::Max(index, data->length() << 1) + 1;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000904 int grow_by = new_size - data->length();
905 data = isolate->factory()->CopyFixedArrayAndGrow(data, grow_by);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000906 env->set_embedder_data(*data);
907 return data;
908}
909
910
911v8::Local<v8::Value> Context::SlowGetEmbedderData(int index) {
912 const char* location = "v8::Context::GetEmbedderData()";
913 i::Handle<i::FixedArray> data = EmbedderDataFor(this, index, false, location);
914 if (data.is_null()) return Local<Value>();
915 i::Handle<i::Object> result(data->get(index), data->GetIsolate());
Steve Blocka7e24c12009-10-30 11:49:00 +0000916 return Utils::ToLocal(result);
917}
918
919
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000920void Context::SetEmbedderData(int index, v8::Local<Value> value) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000921 const char* location = "v8::Context::SetEmbedderData()";
922 i::Handle<i::FixedArray> data = EmbedderDataFor(this, index, true, location);
923 if (data.is_null()) return;
924 i::Handle<i::Object> val = Utils::OpenHandle(*value);
925 data->set(index, *val);
926 DCHECK_EQ(*Utils::OpenHandle(*value),
927 *Utils::OpenHandle(*GetEmbedderData(index)));
928}
Steve Blocka7e24c12009-10-30 11:49:00 +0000929
Steve Blocka7e24c12009-10-30 11:49:00 +0000930
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000931void* Context::SlowGetAlignedPointerFromEmbedderData(int index) {
932 const char* location = "v8::Context::GetAlignedPointerFromEmbedderData()";
933 i::Handle<i::FixedArray> data = EmbedderDataFor(this, index, false, location);
934 if (data.is_null()) return NULL;
935 return DecodeSmiToAligned(data->get(index), location);
936}
Steve Block6ded16b2010-05-10 14:33:55 +0100937
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000938
939void Context::SetAlignedPointerInEmbedderData(int index, void* value) {
940 const char* location = "v8::Context::SetAlignedPointerInEmbedderData()";
941 i::Handle<i::FixedArray> data = EmbedderDataFor(this, index, true, location);
942 data->set(index, EncodeAlignedAsSmi(value, location));
943 DCHECK_EQ(value, GetAlignedPointerFromEmbedderData(index));
Steve Blocka7e24c12009-10-30 11:49:00 +0000944}
945
946
947// --- N e a n d e r ---
948
949
950// A constructor cannot easily return an error value, therefore it is necessary
951// to check for a dead VM with ON_BAILOUT before constructing any Neander
952// objects. To remind you about this there is no HandleScope in the
953// NeanderObject constructor. When you add one to the site calling the
954// constructor you should check that you ensured the VM was not dead first.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000955NeanderObject::NeanderObject(v8::internal::Isolate* isolate, int size) {
Steve Block44f0eee2011-05-26 01:26:41 +0100956 ENTER_V8(isolate);
957 value_ = isolate->factory()->NewNeanderObject();
958 i::Handle<i::FixedArray> elements = isolate->factory()->NewFixedArray(size);
Steve Blocka7e24c12009-10-30 11:49:00 +0000959 value_->set_elements(*elements);
960}
961
962
963int NeanderObject::size() {
964 return i::FixedArray::cast(value_->elements())->length();
965}
966
967
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000968NeanderArray::NeanderArray(v8::internal::Isolate* isolate) : obj_(isolate, 2) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000969 obj_.set(0, i::Smi::FromInt(0));
970}
971
972
973int NeanderArray::length() {
974 return i::Smi::cast(obj_.get(0))->value();
975}
976
977
978i::Object* NeanderArray::get(int offset) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000979 DCHECK_LE(0, offset);
980 DCHECK_LT(offset, length());
Steve Blocka7e24c12009-10-30 11:49:00 +0000981 return obj_.get(offset + 1);
982}
983
984
985// This method cannot easily return an error value, therefore it is necessary
986// to check for a dead VM with ON_BAILOUT before calling it. To remind you
987// about this there is no HandleScope in this method. When you add one to the
988// site calling this method you should check that you ensured the VM was not
989// dead first.
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400990void NeanderArray::add(i::Isolate* isolate, i::Handle<i::Object> value) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000991 int length = this->length();
992 int size = obj_.size();
993 if (length == size - 1) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400994 i::Factory* factory = isolate->factory();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000995 i::Handle<i::FixedArray> new_elms = factory->NewFixedArray(2 * size);
Steve Blocka7e24c12009-10-30 11:49:00 +0000996 for (int i = 0; i < length; i++)
997 new_elms->set(i + 1, get(i));
998 obj_.value()->set_elements(*new_elms);
999 }
1000 obj_.set(length + 1, *value);
1001 obj_.set(0, i::Smi::FromInt(length + 1));
1002}
1003
1004
1005void NeanderArray::set(int index, i::Object* value) {
1006 if (index < 0 || index >= this->length()) return;
1007 obj_.set(index + 1, value);
1008}
1009
1010
1011// --- T e m p l a t e ---
1012
1013
1014static void InitializeTemplate(i::Handle<i::TemplateInfo> that, int type) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001015 that->set_number_of_properties(0);
Steve Blocka7e24c12009-10-30 11:49:00 +00001016 that->set_tag(i::Smi::FromInt(type));
1017}
1018
1019
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001020void Template::Set(v8::Local<Name> name, v8::Local<Data> value,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001021 v8::PropertyAttribute attribute) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001022 auto templ = Utils::OpenHandle(this);
1023 i::Isolate* isolate = templ->GetIsolate();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001024 ENTER_V8(isolate);
1025 i::HandleScope scope(isolate);
Ben Murdochda12d292016-06-02 14:46:10 +01001026 auto value_obj = Utils::OpenHandle(*value);
Ben Murdochc5610432016-08-08 18:44:38 +01001027 CHECK(!value_obj->IsJSReceiver() || value_obj->IsTemplateInfo());
Ben Murdochda12d292016-06-02 14:46:10 +01001028 if (value_obj->IsObjectTemplateInfo()) {
1029 templ->set_serial_number(i::Smi::FromInt(0));
1030 if (templ->IsFunctionTemplateInfo()) {
1031 i::Handle<i::FunctionTemplateInfo>::cast(templ)->set_do_not_cache(true);
1032 }
1033 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001034 i::ApiNatives::AddDataProperty(isolate, templ, Utils::OpenHandle(*name),
Ben Murdochda12d292016-06-02 14:46:10 +01001035 value_obj,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001036 static_cast<i::PropertyAttributes>(attribute));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001037}
1038
1039
1040void Template::SetAccessorProperty(
1041 v8::Local<v8::Name> name,
1042 v8::Local<FunctionTemplate> getter,
1043 v8::Local<FunctionTemplate> setter,
1044 v8::PropertyAttribute attribute,
1045 v8::AccessControl access_control) {
1046 // TODO(verwaest): Remove |access_control|.
1047 DCHECK_EQ(v8::DEFAULT, access_control);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001048 auto templ = Utils::OpenHandle(this);
1049 auto isolate = templ->GetIsolate();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001050 ENTER_V8(isolate);
1051 DCHECK(!name.IsEmpty());
1052 DCHECK(!getter.IsEmpty() || !setter.IsEmpty());
1053 i::HandleScope scope(isolate);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001054 i::ApiNatives::AddAccessorProperty(
1055 isolate, templ, Utils::OpenHandle(*name),
1056 Utils::OpenHandle(*getter, true), Utils::OpenHandle(*setter, true),
1057 static_cast<i::PropertyAttributes>(attribute));
Steve Blocka7e24c12009-10-30 11:49:00 +00001058}
1059
1060
1061// --- F u n c t i o n T e m p l a t e ---
1062static void InitializeFunctionTemplate(
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001063 i::Handle<i::FunctionTemplateInfo> info) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001064 InitializeTemplate(info, Consts::FUNCTION_TEMPLATE);
Steve Blocka7e24c12009-10-30 11:49:00 +00001065 info->set_flag(0);
1066}
1067
Ben Murdoch097c5b22016-05-18 11:27:45 +01001068static Local<ObjectTemplate> ObjectTemplateNew(
1069 i::Isolate* isolate, v8::Local<FunctionTemplate> constructor,
1070 bool do_not_cache);
Steve Blocka7e24c12009-10-30 11:49:00 +00001071
1072Local<ObjectTemplate> FunctionTemplate::PrototypeTemplate() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001073 i::Isolate* i_isolate = Utils::OpenHandle(this)->GetIsolate();
1074 ENTER_V8(i_isolate);
1075 i::Handle<i::Object> result(Utils::OpenHandle(this)->prototype_template(),
1076 i_isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00001077 if (result->IsUndefined()) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01001078 // Do not cache prototype objects.
1079 result = Utils::OpenHandle(
1080 *ObjectTemplateNew(i_isolate, Local<FunctionTemplate>(), true));
Steve Blocka7e24c12009-10-30 11:49:00 +00001081 Utils::OpenHandle(this)->set_prototype_template(*result);
1082 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001083 return ToApiHandle<ObjectTemplate>(result);
Steve Blocka7e24c12009-10-30 11:49:00 +00001084}
1085
1086
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001087static void EnsureNotInstantiated(i::Handle<i::FunctionTemplateInfo> info,
1088 const char* func) {
1089 Utils::ApiCheck(!info->instantiated(), func,
1090 "FunctionTemplate already instantiated");
1091}
1092
1093
1094void FunctionTemplate::Inherit(v8::Local<FunctionTemplate> value) {
1095 auto info = Utils::OpenHandle(this);
1096 EnsureNotInstantiated(info, "v8::FunctionTemplate::Inherit");
1097 i::Isolate* isolate = info->GetIsolate();
Steve Block44f0eee2011-05-26 01:26:41 +01001098 ENTER_V8(isolate);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001099 info->set_parent_template(*Utils::OpenHandle(*value));
Steve Blocka7e24c12009-10-30 11:49:00 +00001100}
1101
1102
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001103static Local<FunctionTemplate> FunctionTemplateNew(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001104 i::Isolate* isolate, FunctionCallback callback,
1105 experimental::FastAccessorBuilder* fast_handler, v8::Local<Value> data,
1106 v8::Local<Signature> signature, int length, bool do_not_cache) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001107 i::Handle<i::Struct> struct_obj =
Steve Block44f0eee2011-05-26 01:26:41 +01001108 isolate->factory()->NewStruct(i::FUNCTION_TEMPLATE_INFO_TYPE);
Steve Blocka7e24c12009-10-30 11:49:00 +00001109 i::Handle<i::FunctionTemplateInfo> obj =
1110 i::Handle<i::FunctionTemplateInfo>::cast(struct_obj);
1111 InitializeFunctionTemplate(obj);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001112 obj->set_do_not_cache(do_not_cache);
1113 int next_serial_number = 0;
1114 if (!do_not_cache) {
1115 next_serial_number = isolate->next_serial_number() + 1;
1116 isolate->set_next_serial_number(next_serial_number);
1117 }
Steve Block44f0eee2011-05-26 01:26:41 +01001118 obj->set_serial_number(i::Smi::FromInt(next_serial_number));
Steve Blocka7e24c12009-10-30 11:49:00 +00001119 if (callback != 0) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001120 if (data.IsEmpty()) {
1121 data = v8::Undefined(reinterpret_cast<v8::Isolate*>(isolate));
1122 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001123 Utils::ToLocal(obj)->SetCallHandler(callback, data, fast_handler);
Steve Blocka7e24c12009-10-30 11:49:00 +00001124 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001125 obj->set_length(length);
Steve Blocka7e24c12009-10-30 11:49:00 +00001126 obj->set_undetectable(false);
1127 obj->set_needs_access_check(false);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001128 obj->set_accept_any_receiver(true);
Steve Blocka7e24c12009-10-30 11:49:00 +00001129 if (!signature.IsEmpty())
1130 obj->set_signature(*Utils::OpenHandle(*signature));
1131 return Utils::ToLocal(obj);
1132}
1133
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001134
Ben Murdochc5610432016-08-08 18:44:38 +01001135Local<FunctionTemplate> FunctionTemplate::New(
1136 Isolate* isolate, FunctionCallback callback, v8::Local<Value> data,
1137 v8::Local<Signature> signature, int length, ConstructorBehavior behavior) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001138 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001139 // Changes to the environment cannot be captured in the snapshot. Expect no
1140 // function templates when the isolate is created for serialization.
1141 DCHECK(!i_isolate->serializer_enabled());
Ben Murdochc5610432016-08-08 18:44:38 +01001142 LOG_API(i_isolate, FunctionTemplate, New);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001143 ENTER_V8(i_isolate);
Ben Murdochc5610432016-08-08 18:44:38 +01001144 auto templ = FunctionTemplateNew(i_isolate, callback, nullptr, data,
1145 signature, length, false);
1146 if (behavior == ConstructorBehavior::kThrow) templ->RemovePrototype();
1147 return templ;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001148}
1149
1150
1151Local<FunctionTemplate> FunctionTemplate::NewWithFastHandler(
1152 Isolate* isolate, FunctionCallback callback,
1153 experimental::FastAccessorBuilder* fast_handler, v8::Local<Value> data,
1154 v8::Local<Signature> signature, int length) {
1155 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
1156 DCHECK(!i_isolate->serializer_enabled());
Ben Murdochc5610432016-08-08 18:44:38 +01001157 LOG_API(i_isolate, FunctionTemplate, NewWithFastHandler);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001158 ENTER_V8(i_isolate);
1159 return FunctionTemplateNew(i_isolate, callback, fast_handler, data, signature,
1160 length, false);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001161}
Steve Blocka7e24c12009-10-30 11:49:00 +00001162
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001163
1164Local<Signature> Signature::New(Isolate* isolate,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001165 Local<FunctionTemplate> receiver) {
1166 return Utils::SignatureToLocal(Utils::OpenHandle(*receiver));
Steve Blocka7e24c12009-10-30 11:49:00 +00001167}
1168
1169
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001170Local<AccessorSignature> AccessorSignature::New(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001171 Isolate* isolate, Local<FunctionTemplate> receiver) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001172 return Utils::AccessorSignatureToLocal(Utils::OpenHandle(*receiver));
1173}
1174
1175
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001176#define SET_FIELD_WRAPPED(obj, setter, cdata) do { \
1177 i::Handle<i::Object> foreign = FromCData(obj->GetIsolate(), cdata); \
1178 (obj)->setter(*foreign); \
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001179 } while (false)
1180
1181
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001182void FunctionTemplate::SetCallHandler(
1183 FunctionCallback callback, v8::Local<Value> data,
1184 experimental::FastAccessorBuilder* fast_handler) {
1185 auto info = Utils::OpenHandle(this);
1186 EnsureNotInstantiated(info, "v8::FunctionTemplate::SetCallHandler");
1187 i::Isolate* isolate = info->GetIsolate();
Steve Block44f0eee2011-05-26 01:26:41 +01001188 ENTER_V8(isolate);
1189 i::HandleScope scope(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00001190 i::Handle<i::Struct> struct_obj =
Steve Block44f0eee2011-05-26 01:26:41 +01001191 isolate->factory()->NewStruct(i::CALL_HANDLER_INFO_TYPE);
Steve Blocka7e24c12009-10-30 11:49:00 +00001192 i::Handle<i::CallHandlerInfo> obj =
1193 i::Handle<i::CallHandlerInfo>::cast(struct_obj);
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001194 SET_FIELD_WRAPPED(obj, set_callback, callback);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001195 i::MaybeHandle<i::Code> code =
1196 i::experimental::BuildCodeFromFastAccessorBuilder(fast_handler);
1197 if (!code.is_null()) {
1198 obj->set_fast_handler(*code.ToHandleChecked());
1199 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001200 if (data.IsEmpty()) {
1201 data = v8::Undefined(reinterpret_cast<v8::Isolate*>(isolate));
1202 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001203 obj->set_data(*Utils::OpenHandle(*data));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001204 info->set_call_code(*obj);
Steve Blocka7e24c12009-10-30 11:49:00 +00001205}
1206
1207
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001208static i::Handle<i::AccessorInfo> SetAccessorInfoProperties(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001209 i::Handle<i::AccessorInfo> obj, v8::Local<Name> name,
1210 v8::AccessControl settings, v8::PropertyAttribute attributes,
1211 v8::Local<AccessorSignature> signature) {
Leon Clarkef7060e22010-06-03 12:02:55 +01001212 obj->set_name(*Utils::OpenHandle(*name));
1213 if (settings & ALL_CAN_READ) obj->set_all_can_read(true);
1214 if (settings & ALL_CAN_WRITE) obj->set_all_can_write(true);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001215 obj->set_property_attributes(static_cast<i::PropertyAttributes>(attributes));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001216 if (!signature.IsEmpty()) {
1217 obj->set_expected_receiver_type(*Utils::OpenHandle(*signature));
1218 }
Leon Clarkef7060e22010-06-03 12:02:55 +01001219 return obj;
1220}
1221
Ben Murdochc5610432016-08-08 18:44:38 +01001222namespace {
1223
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001224template <typename Getter, typename Setter>
Ben Murdochc5610432016-08-08 18:44:38 +01001225i::Handle<i::AccessorInfo> MakeAccessorInfo(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001226 v8::Local<Name> name, Getter getter, Setter setter, v8::Local<Value> data,
1227 v8::AccessControl settings, v8::PropertyAttribute attributes,
Ben Murdoch097c5b22016-05-18 11:27:45 +01001228 v8::Local<AccessorSignature> signature, bool is_special_data_property) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001229 i::Isolate* isolate = Utils::OpenHandle(*name)->GetIsolate();
Ben Murdoch097c5b22016-05-18 11:27:45 +01001230 i::Handle<i::AccessorInfo> obj = isolate->factory()->NewAccessorInfo();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001231 SET_FIELD_WRAPPED(obj, set_getter, getter);
Ben Murdoch097c5b22016-05-18 11:27:45 +01001232 if (is_special_data_property && setter == nullptr) {
1233 setter = reinterpret_cast<Setter>(&i::Accessors::ReconfigureToDataProperty);
1234 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001235 SET_FIELD_WRAPPED(obj, set_setter, setter);
Ben Murdochc5610432016-08-08 18:44:38 +01001236 i::Address redirected = obj->redirected_getter();
1237 if (redirected != nullptr) SET_FIELD_WRAPPED(obj, set_js_getter, redirected);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001238 if (data.IsEmpty()) {
1239 data = v8::Undefined(reinterpret_cast<v8::Isolate*>(isolate));
Steve Blocka7e24c12009-10-30 11:49:00 +00001240 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001241 obj->set_data(*Utils::OpenHandle(*data));
Ben Murdoch097c5b22016-05-18 11:27:45 +01001242 obj->set_is_special_data_property(is_special_data_property);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001243 return SetAccessorInfoProperties(obj, name, settings, attributes, signature);
1244}
Steve Blocka7e24c12009-10-30 11:49:00 +00001245
Ben Murdochc5610432016-08-08 18:44:38 +01001246} // namespace
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001247
Steve Blocka7e24c12009-10-30 11:49:00 +00001248Local<ObjectTemplate> FunctionTemplate::InstanceTemplate() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001249 i::Handle<i::FunctionTemplateInfo> handle = Utils::OpenHandle(this, true);
1250 if (!Utils::ApiCheck(!handle.is_null(),
1251 "v8::FunctionTemplate::InstanceTemplate()",
1252 "Reading from empty handle")) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001253 return Local<ObjectTemplate>();
Steve Blocka7e24c12009-10-30 11:49:00 +00001254 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001255 i::Isolate* isolate = handle->GetIsolate();
1256 ENTER_V8(isolate);
1257 if (handle->instance_template()->IsUndefined()) {
1258 Local<ObjectTemplate> templ =
1259 ObjectTemplate::New(isolate, ToApiHandle<FunctionTemplate>(handle));
1260 handle->set_instance_template(*Utils::OpenHandle(*templ));
1261 }
1262 i::Handle<i::ObjectTemplateInfo> result(
1263 i::ObjectTemplateInfo::cast(handle->instance_template()));
Steve Blocka7e24c12009-10-30 11:49:00 +00001264 return Utils::ToLocal(result);
1265}
1266
1267
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001268void FunctionTemplate::SetLength(int length) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001269 auto info = Utils::OpenHandle(this);
1270 EnsureNotInstantiated(info, "v8::FunctionTemplate::SetLength");
1271 auto isolate = info->GetIsolate();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001272 ENTER_V8(isolate);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001273 info->set_length(length);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001274}
1275
1276
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001277void FunctionTemplate::SetClassName(Local<String> name) {
1278 auto info = Utils::OpenHandle(this);
1279 EnsureNotInstantiated(info, "v8::FunctionTemplate::SetClassName");
1280 auto isolate = info->GetIsolate();
Steve Block44f0eee2011-05-26 01:26:41 +01001281 ENTER_V8(isolate);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001282 info->set_class_name(*Utils::OpenHandle(*name));
1283}
1284
1285
1286void FunctionTemplate::SetAcceptAnyReceiver(bool value) {
1287 auto info = Utils::OpenHandle(this);
1288 EnsureNotInstantiated(info, "v8::FunctionTemplate::SetAcceptAnyReceiver");
1289 auto isolate = info->GetIsolate();
1290 ENTER_V8(isolate);
1291 info->set_accept_any_receiver(value);
Steve Blocka7e24c12009-10-30 11:49:00 +00001292}
1293
1294
1295void FunctionTemplate::SetHiddenPrototype(bool value) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001296 auto info = Utils::OpenHandle(this);
1297 EnsureNotInstantiated(info, "v8::FunctionTemplate::SetHiddenPrototype");
1298 auto isolate = info->GetIsolate();
Steve Block44f0eee2011-05-26 01:26:41 +01001299 ENTER_V8(isolate);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001300 info->set_hidden_prototype(value);
Steve Blocka7e24c12009-10-30 11:49:00 +00001301}
1302
1303
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001304void FunctionTemplate::ReadOnlyPrototype() {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001305 auto info = Utils::OpenHandle(this);
1306 EnsureNotInstantiated(info, "v8::FunctionTemplate::ReadOnlyPrototype");
1307 auto isolate = info->GetIsolate();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001308 ENTER_V8(isolate);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001309 info->set_read_only_prototype(true);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001310}
1311
1312
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001313void FunctionTemplate::RemovePrototype() {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001314 auto info = Utils::OpenHandle(this);
1315 EnsureNotInstantiated(info, "v8::FunctionTemplate::RemovePrototype");
1316 auto isolate = info->GetIsolate();
Steve Block44f0eee2011-05-26 01:26:41 +01001317 ENTER_V8(isolate);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001318 info->set_remove_prototype(true);
Steve Blocka7e24c12009-10-30 11:49:00 +00001319}
1320
1321
1322// --- O b j e c t T e m p l a t e ---
1323
1324
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001325Local<ObjectTemplate> ObjectTemplate::New(
1326 Isolate* isolate, v8::Local<FunctionTemplate> constructor) {
1327 return New(reinterpret_cast<i::Isolate*>(isolate), constructor);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001328}
1329
1330
Steve Blocka7e24c12009-10-30 11:49:00 +00001331Local<ObjectTemplate> ObjectTemplate::New() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001332 return New(i::Isolate::Current(), Local<FunctionTemplate>());
Steve Blocka7e24c12009-10-30 11:49:00 +00001333}
1334
Ben Murdoch097c5b22016-05-18 11:27:45 +01001335static Local<ObjectTemplate> ObjectTemplateNew(
1336 i::Isolate* isolate, v8::Local<FunctionTemplate> constructor,
1337 bool do_not_cache) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001338 // Changes to the environment cannot be captured in the snapshot. Expect no
1339 // object templates when the isolate is created for serialization.
1340 DCHECK(!isolate->serializer_enabled());
Ben Murdochc5610432016-08-08 18:44:38 +01001341 LOG_API(isolate, ObjectTemplate, New);
Steve Block44f0eee2011-05-26 01:26:41 +01001342 ENTER_V8(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00001343 i::Handle<i::Struct> struct_obj =
Steve Block44f0eee2011-05-26 01:26:41 +01001344 isolate->factory()->NewStruct(i::OBJECT_TEMPLATE_INFO_TYPE);
Steve Blocka7e24c12009-10-30 11:49:00 +00001345 i::Handle<i::ObjectTemplateInfo> obj =
1346 i::Handle<i::ObjectTemplateInfo>::cast(struct_obj);
1347 InitializeTemplate(obj, Consts::OBJECT_TEMPLATE);
Ben Murdoch097c5b22016-05-18 11:27:45 +01001348 int next_serial_number = 0;
1349 if (!do_not_cache) {
1350 next_serial_number = isolate->next_serial_number() + 1;
1351 isolate->set_next_serial_number(next_serial_number);
1352 }
1353 obj->set_serial_number(i::Smi::FromInt(next_serial_number));
Steve Blocka7e24c12009-10-30 11:49:00 +00001354 if (!constructor.IsEmpty())
1355 obj->set_constructor(*Utils::OpenHandle(*constructor));
1356 obj->set_internal_field_count(i::Smi::FromInt(0));
1357 return Utils::ToLocal(obj);
1358}
1359
Ben Murdoch097c5b22016-05-18 11:27:45 +01001360Local<ObjectTemplate> ObjectTemplate::New(
1361 i::Isolate* isolate, v8::Local<FunctionTemplate> constructor) {
1362 return ObjectTemplateNew(isolate, constructor, false);
1363}
Steve Blocka7e24c12009-10-30 11:49:00 +00001364
1365// Ensure that the object template has a constructor. If no
1366// constructor is available we create one.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001367static i::Handle<i::FunctionTemplateInfo> EnsureConstructor(
1368 i::Isolate* isolate,
1369 ObjectTemplate* object_template) {
1370 i::Object* obj = Utils::OpenHandle(object_template)->constructor();
1371 if (!obj ->IsUndefined()) {
1372 i::FunctionTemplateInfo* info = i::FunctionTemplateInfo::cast(obj);
1373 return i::Handle<i::FunctionTemplateInfo>(info, isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00001374 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001375 Local<FunctionTemplate> templ =
1376 FunctionTemplate::New(reinterpret_cast<Isolate*>(isolate));
1377 i::Handle<i::FunctionTemplateInfo> constructor = Utils::OpenHandle(*templ);
1378 constructor->set_instance_template(*Utils::OpenHandle(object_template));
1379 Utils::OpenHandle(object_template)->set_constructor(*constructor);
1380 return constructor;
1381}
1382
1383
Ben Murdoch097c5b22016-05-18 11:27:45 +01001384template <typename Getter, typename Setter, typename Data, typename Template>
1385static bool TemplateSetAccessor(Template* template_obj, v8::Local<Name> name,
1386 Getter getter, Setter setter, Data data,
1387 AccessControl settings,
1388 PropertyAttribute attribute,
1389 v8::Local<AccessorSignature> signature,
1390 bool is_special_data_property) {
1391 auto info = Utils::OpenHandle(template_obj);
1392 auto isolate = info->GetIsolate();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001393 ENTER_V8(isolate);
1394 i::HandleScope scope(isolate);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001395 auto obj = MakeAccessorInfo(name, getter, setter, data, settings, attribute,
Ben Murdoch097c5b22016-05-18 11:27:45 +01001396 signature, is_special_data_property);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001397 if (obj.is_null()) return false;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001398 i::ApiNatives::AddNativeDataProperty(isolate, info, obj);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001399 return true;
1400}
1401
1402
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001403void Template::SetNativeDataProperty(v8::Local<String> name,
1404 AccessorGetterCallback getter,
1405 AccessorSetterCallback setter,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001406 v8::Local<Value> data,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001407 PropertyAttribute attribute,
1408 v8::Local<AccessorSignature> signature,
1409 AccessControl settings) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01001410 TemplateSetAccessor(this, name, getter, setter, data, settings, attribute,
1411 signature, true);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001412}
1413
1414
1415void Template::SetNativeDataProperty(v8::Local<Name> name,
1416 AccessorNameGetterCallback getter,
1417 AccessorNameSetterCallback setter,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001418 v8::Local<Value> data,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001419 PropertyAttribute attribute,
1420 v8::Local<AccessorSignature> signature,
1421 AccessControl settings) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01001422 TemplateSetAccessor(this, name, getter, setter, data, settings, attribute,
1423 signature, true);
Steve Blocka7e24c12009-10-30 11:49:00 +00001424}
1425
1426
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001427void Template::SetIntrinsicDataProperty(Local<Name> name, Intrinsic intrinsic,
1428 PropertyAttribute attribute) {
1429 auto templ = Utils::OpenHandle(this);
1430 i::Isolate* isolate = templ->GetIsolate();
1431 ENTER_V8(isolate);
1432 i::HandleScope scope(isolate);
1433 i::ApiNatives::AddDataProperty(isolate, templ, Utils::OpenHandle(*name),
1434 intrinsic,
1435 static_cast<i::PropertyAttributes>(attribute));
1436}
1437
1438
1439void ObjectTemplate::SetAccessor(v8::Local<String> name,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001440 AccessorGetterCallback getter,
1441 AccessorSetterCallback setter,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001442 v8::Local<Value> data, AccessControl settings,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001443 PropertyAttribute attribute,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001444 v8::Local<AccessorSignature> signature) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01001445 TemplateSetAccessor(this, name, getter, setter, data, settings, attribute,
1446 signature, i::FLAG_disable_old_api_accessors);
Steve Blocka7e24c12009-10-30 11:49:00 +00001447}
1448
1449
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001450void ObjectTemplate::SetAccessor(v8::Local<Name> name,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001451 AccessorNameGetterCallback getter,
1452 AccessorNameSetterCallback setter,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001453 v8::Local<Value> data, AccessControl settings,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001454 PropertyAttribute attribute,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001455 v8::Local<AccessorSignature> signature) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01001456 TemplateSetAccessor(this, name, getter, setter, data, settings, attribute,
1457 signature, i::FLAG_disable_old_api_accessors);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001458}
1459
1460
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001461template <typename Getter, typename Setter, typename Query, typename Deleter,
1462 typename Enumerator>
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001463static void ObjectTemplateSetNamedPropertyHandler(ObjectTemplate* templ,
1464 Getter getter, Setter setter,
1465 Query query, Deleter remover,
1466 Enumerator enumerator,
1467 Local<Value> data,
1468 PropertyHandlerFlags flags) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001469 i::Isolate* isolate = Utils::OpenHandle(templ)->GetIsolate();
Steve Block44f0eee2011-05-26 01:26:41 +01001470 ENTER_V8(isolate);
1471 i::HandleScope scope(isolate);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001472 auto cons = EnsureConstructor(isolate, templ);
1473 EnsureNotInstantiated(cons, "ObjectTemplateSetNamedPropertyHandler");
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001474 auto obj = i::Handle<i::InterceptorInfo>::cast(
1475 isolate->factory()->NewStruct(i::INTERCEPTOR_INFO_TYPE));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001476 obj->set_flags(0);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001477
1478 if (getter != 0) SET_FIELD_WRAPPED(obj, set_getter, getter);
1479 if (setter != 0) SET_FIELD_WRAPPED(obj, set_setter, setter);
1480 if (query != 0) SET_FIELD_WRAPPED(obj, set_query, query);
1481 if (remover != 0) SET_FIELD_WRAPPED(obj, set_deleter, remover);
1482 if (enumerator != 0) SET_FIELD_WRAPPED(obj, set_enumerator, enumerator);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001483 obj->set_can_intercept_symbols(
1484 !(static_cast<int>(flags) &
1485 static_cast<int>(PropertyHandlerFlags::kOnlyInterceptStrings)));
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001486 obj->set_all_can_read(static_cast<int>(flags) &
1487 static_cast<int>(PropertyHandlerFlags::kAllCanRead));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001488 obj->set_non_masking(static_cast<int>(flags) &
1489 static_cast<int>(PropertyHandlerFlags::kNonMasking));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001490
1491 if (data.IsEmpty()) {
1492 data = v8::Undefined(reinterpret_cast<v8::Isolate*>(isolate));
1493 }
1494 obj->set_data(*Utils::OpenHandle(*data));
1495 cons->set_named_property_handler(*obj);
Steve Blocka7e24c12009-10-30 11:49:00 +00001496}
1497
1498
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001499void ObjectTemplate::SetNamedPropertyHandler(
1500 NamedPropertyGetterCallback getter, NamedPropertySetterCallback setter,
1501 NamedPropertyQueryCallback query, NamedPropertyDeleterCallback remover,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001502 NamedPropertyEnumeratorCallback enumerator, Local<Value> data) {
1503 ObjectTemplateSetNamedPropertyHandler(
1504 this, getter, setter, query, remover, enumerator, data,
1505 PropertyHandlerFlags::kOnlyInterceptStrings);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001506}
1507
1508
1509void ObjectTemplate::SetHandler(
1510 const NamedPropertyHandlerConfiguration& config) {
1511 ObjectTemplateSetNamedPropertyHandler(
1512 this, config.getter, config.setter, config.query, config.deleter,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001513 config.enumerator, config.data, config.flags);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001514}
1515
1516
Steve Blocka7e24c12009-10-30 11:49:00 +00001517void ObjectTemplate::MarkAsUndetectable() {
Steve Block44f0eee2011-05-26 01:26:41 +01001518 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
Steve Block44f0eee2011-05-26 01:26:41 +01001519 ENTER_V8(isolate);
1520 i::HandleScope scope(isolate);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001521 auto cons = EnsureConstructor(isolate, this);
1522 EnsureNotInstantiated(cons, "v8::ObjectTemplate::MarkAsUndetectable");
Steve Blocka7e24c12009-10-30 11:49:00 +00001523 cons->set_undetectable(true);
1524}
1525
1526
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001527void ObjectTemplate::SetAccessCheckCallback(AccessCheckCallback callback,
1528 Local<Value> data) {
Steve Block44f0eee2011-05-26 01:26:41 +01001529 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
Steve Block44f0eee2011-05-26 01:26:41 +01001530 ENTER_V8(isolate);
1531 i::HandleScope scope(isolate);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001532 auto cons = EnsureConstructor(isolate, this);
1533 EnsureNotInstantiated(cons, "v8::ObjectTemplate::SetAccessCheckCallback");
Steve Blocka7e24c12009-10-30 11:49:00 +00001534
1535 i::Handle<i::Struct> struct_info =
Steve Block44f0eee2011-05-26 01:26:41 +01001536 isolate->factory()->NewStruct(i::ACCESS_CHECK_INFO_TYPE);
Steve Blocka7e24c12009-10-30 11:49:00 +00001537 i::Handle<i::AccessCheckInfo> info =
1538 i::Handle<i::AccessCheckInfo>::cast(struct_info);
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001539
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001540 SET_FIELD_WRAPPED(info, set_callback, callback);
1541 SET_FIELD_WRAPPED(info, set_named_callback, nullptr);
1542 SET_FIELD_WRAPPED(info, set_indexed_callback, nullptr);
1543
1544 if (data.IsEmpty()) {
1545 data = v8::Undefined(reinterpret_cast<v8::Isolate*>(isolate));
1546 }
1547 info->set_data(*Utils::OpenHandle(*data));
1548
1549 cons->set_access_check_info(*info);
1550 cons->set_needs_access_check(true);
1551}
1552
Ben Murdoch097c5b22016-05-18 11:27:45 +01001553void ObjectTemplate::SetAccessCheckCallback(
1554 DeprecatedAccessCheckCallback callback, Local<Value> data) {
1555 SetAccessCheckCallback(reinterpret_cast<AccessCheckCallback>(callback), data);
1556}
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001557
1558void ObjectTemplate::SetAccessCheckCallbacks(
1559 NamedSecurityCallback named_callback,
1560 IndexedSecurityCallback indexed_callback, Local<Value> data) {
1561 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
1562 ENTER_V8(isolate);
1563 i::HandleScope scope(isolate);
1564 auto cons = EnsureConstructor(isolate, this);
1565 EnsureNotInstantiated(cons, "v8::ObjectTemplate::SetAccessCheckCallbacks");
1566
1567 i::Handle<i::Struct> struct_info =
1568 isolate->factory()->NewStruct(i::ACCESS_CHECK_INFO_TYPE);
1569 i::Handle<i::AccessCheckInfo> info =
1570 i::Handle<i::AccessCheckInfo>::cast(struct_info);
1571
1572 SET_FIELD_WRAPPED(info, set_callback, nullptr);
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001573 SET_FIELD_WRAPPED(info, set_named_callback, named_callback);
1574 SET_FIELD_WRAPPED(info, set_indexed_callback, indexed_callback);
1575
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001576 if (data.IsEmpty()) {
1577 data = v8::Undefined(reinterpret_cast<v8::Isolate*>(isolate));
1578 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001579 info->set_data(*Utils::OpenHandle(*data));
1580
Steve Blocka7e24c12009-10-30 11:49:00 +00001581 cons->set_access_check_info(*info);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001582 cons->set_needs_access_check(true);
Steve Blocka7e24c12009-10-30 11:49:00 +00001583}
1584
1585
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001586void ObjectTemplate::SetHandler(
1587 const IndexedPropertyHandlerConfiguration& config) {
Steve Block44f0eee2011-05-26 01:26:41 +01001588 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
Steve Block44f0eee2011-05-26 01:26:41 +01001589 ENTER_V8(isolate);
1590 i::HandleScope scope(isolate);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001591 auto cons = EnsureConstructor(isolate, this);
1592 EnsureNotInstantiated(cons, "v8::ObjectTemplate::SetHandler");
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001593 auto obj = i::Handle<i::InterceptorInfo>::cast(
1594 isolate->factory()->NewStruct(i::INTERCEPTOR_INFO_TYPE));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001595 obj->set_flags(0);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001596
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001597 if (config.getter != 0) SET_FIELD_WRAPPED(obj, set_getter, config.getter);
1598 if (config.setter != 0) SET_FIELD_WRAPPED(obj, set_setter, config.setter);
1599 if (config.query != 0) SET_FIELD_WRAPPED(obj, set_query, config.query);
1600 if (config.deleter != 0) SET_FIELD_WRAPPED(obj, set_deleter, config.deleter);
1601 if (config.enumerator != 0) {
1602 SET_FIELD_WRAPPED(obj, set_enumerator, config.enumerator);
1603 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001604 obj->set_all_can_read(static_cast<int>(config.flags) &
1605 static_cast<int>(PropertyHandlerFlags::kAllCanRead));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001606
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001607 v8::Local<v8::Value> data = config.data;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001608 if (data.IsEmpty()) {
1609 data = v8::Undefined(reinterpret_cast<v8::Isolate*>(isolate));
1610 }
1611 obj->set_data(*Utils::OpenHandle(*data));
1612 cons->set_indexed_property_handler(*obj);
Steve Blocka7e24c12009-10-30 11:49:00 +00001613}
1614
1615
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001616void ObjectTemplate::SetCallAsFunctionHandler(FunctionCallback callback,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001617 Local<Value> data) {
Steve Block44f0eee2011-05-26 01:26:41 +01001618 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
Steve Block44f0eee2011-05-26 01:26:41 +01001619 ENTER_V8(isolate);
1620 i::HandleScope scope(isolate);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001621 auto cons = EnsureConstructor(isolate, this);
1622 EnsureNotInstantiated(cons, "v8::ObjectTemplate::SetCallAsFunctionHandler");
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001623 i::Handle<i::Struct> struct_obj =
1624 isolate->factory()->NewStruct(i::CALL_HANDLER_INFO_TYPE);
1625 i::Handle<i::CallHandlerInfo> obj =
1626 i::Handle<i::CallHandlerInfo>::cast(struct_obj);
1627 SET_FIELD_WRAPPED(obj, set_callback, callback);
1628 if (data.IsEmpty()) {
1629 data = v8::Undefined(reinterpret_cast<v8::Isolate*>(isolate));
1630 }
1631 obj->set_data(*Utils::OpenHandle(*data));
1632 cons->set_instance_call_handler(*obj);
Steve Blocka7e24c12009-10-30 11:49:00 +00001633}
1634
1635
1636int ObjectTemplate::InternalFieldCount() {
Steve Blocka7e24c12009-10-30 11:49:00 +00001637 return i::Smi::cast(Utils::OpenHandle(this)->internal_field_count())->value();
1638}
1639
1640
1641void ObjectTemplate::SetInternalFieldCount(int value) {
Steve Block44f0eee2011-05-26 01:26:41 +01001642 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001643 if (!Utils::ApiCheck(i::Smi::IsValid(value),
1644 "v8::ObjectTemplate::SetInternalFieldCount()",
1645 "Invalid internal field count")) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001646 return;
1647 }
Steve Block44f0eee2011-05-26 01:26:41 +01001648 ENTER_V8(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00001649 if (value > 0) {
1650 // The internal field count is set by the constructor function's
1651 // construct code, so we ensure that there is a constructor
1652 // function to do the setting.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001653 EnsureConstructor(isolate, this);
Steve Blocka7e24c12009-10-30 11:49:00 +00001654 }
1655 Utils::OpenHandle(this)->set_internal_field_count(i::Smi::FromInt(value));
1656}
1657
1658
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001659// --- S c r i p t s ---
Steve Blocka7e24c12009-10-30 11:49:00 +00001660
1661
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001662// Internally, UnboundScript is a SharedFunctionInfo, and Script is a
1663// JSFunction.
1664
1665ScriptCompiler::CachedData::CachedData(const uint8_t* data_, int length_,
1666 BufferPolicy buffer_policy_)
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001667 : data(data_),
1668 length(length_),
1669 rejected(false),
1670 buffer_policy(buffer_policy_) {}
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001671
1672
1673ScriptCompiler::CachedData::~CachedData() {
1674 if (buffer_policy == BufferOwned) {
1675 delete[] data;
1676 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001677}
1678
1679
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001680bool ScriptCompiler::ExternalSourceStream::SetBookmark() { return false; }
1681
1682
1683void ScriptCompiler::ExternalSourceStream::ResetToBookmark() { UNREACHABLE(); }
1684
1685
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001686ScriptCompiler::StreamedSource::StreamedSource(ExternalSourceStream* stream,
1687 Encoding encoding)
1688 : impl_(new i::StreamedSource(stream, encoding)) {}
1689
1690
1691ScriptCompiler::StreamedSource::~StreamedSource() { delete impl_; }
1692
1693
1694const ScriptCompiler::CachedData*
1695ScriptCompiler::StreamedSource::GetCachedData() const {
1696 return impl_->cached_data.get();
1697}
1698
1699
1700Local<Script> UnboundScript::BindToCurrentContext() {
1701 i::Handle<i::HeapObject> obj =
1702 i::Handle<i::HeapObject>::cast(Utils::OpenHandle(this));
1703 i::Handle<i::SharedFunctionInfo>
1704 function_info(i::SharedFunctionInfo::cast(*obj), obj->GetIsolate());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001705 i::Isolate* isolate = obj->GetIsolate();
1706
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001707 i::Handle<i::JSReceiver> global(isolate->native_context()->global_object());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001708 i::Handle<i::JSFunction> function =
1709 obj->GetIsolate()->factory()->NewFunctionFromSharedFunctionInfo(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001710 function_info, isolate->native_context());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001711 return ToApiHandle<Script>(function);
1712}
1713
1714
1715int UnboundScript::GetId() {
1716 i::Handle<i::HeapObject> obj =
1717 i::Handle<i::HeapObject>::cast(Utils::OpenHandle(this));
1718 i::Isolate* isolate = obj->GetIsolate();
Ben Murdochc5610432016-08-08 18:44:38 +01001719 LOG_API(isolate, UnboundScript, GetId);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001720 i::HandleScope scope(isolate);
1721 i::Handle<i::SharedFunctionInfo> function_info(
1722 i::SharedFunctionInfo::cast(*obj));
1723 i::Handle<i::Script> script(i::Script::cast(function_info->script()));
1724 return script->id();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001725}
1726
1727
1728int UnboundScript::GetLineNumber(int code_pos) {
1729 i::Handle<i::SharedFunctionInfo> obj =
1730 i::Handle<i::SharedFunctionInfo>::cast(Utils::OpenHandle(this));
1731 i::Isolate* isolate = obj->GetIsolate();
Ben Murdochc5610432016-08-08 18:44:38 +01001732 LOG_API(isolate, UnboundScript, GetLineNumber);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001733 if (obj->script()->IsScript()) {
1734 i::Handle<i::Script> script(i::Script::cast(obj->script()));
1735 return i::Script::GetLineNumber(script, code_pos);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001736 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001737 return -1;
Ben Murdochb0fe1622011-05-05 13:52:32 +01001738 }
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001739}
1740
1741
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001742Local<Value> UnboundScript::GetScriptName() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001743 i::Handle<i::SharedFunctionInfo> obj =
1744 i::Handle<i::SharedFunctionInfo>::cast(Utils::OpenHandle(this));
1745 i::Isolate* isolate = obj->GetIsolate();
Ben Murdochc5610432016-08-08 18:44:38 +01001746 LOG_API(isolate, UnboundScript, GetName);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001747 if (obj->script()->IsScript()) {
1748 i::Object* name = i::Script::cast(obj->script())->name();
1749 return Utils::ToLocal(i::Handle<i::Object>(name, isolate));
1750 } else {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001751 return Local<String>();
Leon Clarkef7060e22010-06-03 12:02:55 +01001752 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001753}
Leon Clarkef7060e22010-06-03 12:02:55 +01001754
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001755
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001756Local<Value> UnboundScript::GetSourceURL() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001757 i::Handle<i::SharedFunctionInfo> obj =
1758 i::Handle<i::SharedFunctionInfo>::cast(Utils::OpenHandle(this));
1759 i::Isolate* isolate = obj->GetIsolate();
Ben Murdochc5610432016-08-08 18:44:38 +01001760 LOG_API(isolate, UnboundScript, GetSourceURL);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001761 if (obj->script()->IsScript()) {
1762 i::Object* url = i::Script::cast(obj->script())->source_url();
1763 return Utils::ToLocal(i::Handle<i::Object>(url, isolate));
1764 } else {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001765 return Local<String>();
Iain Merrick9ac36c92010-09-13 15:29:50 +01001766 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001767}
1768
1769
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001770Local<Value> UnboundScript::GetSourceMappingURL() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001771 i::Handle<i::SharedFunctionInfo> obj =
1772 i::Handle<i::SharedFunctionInfo>::cast(Utils::OpenHandle(this));
1773 i::Isolate* isolate = obj->GetIsolate();
Ben Murdochc5610432016-08-08 18:44:38 +01001774 LOG_API(isolate, UnboundScript, GetSourceMappingURL);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001775 if (obj->script()->IsScript()) {
1776 i::Object* url = i::Script::cast(obj->script())->source_mapping_url();
1777 return Utils::ToLocal(i::Handle<i::Object>(url, isolate));
1778 } else {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001779 return Local<String>();
Ben Murdoch692be652012-01-10 18:47:50 +00001780 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001781}
1782
1783
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001784MaybeLocal<Value> Script::Run(Local<Context> context) {
Ben Murdochc5610432016-08-08 18:44:38 +01001785 PREPARE_FOR_EXECUTION_WITH_CALLBACK(context, Script, Run, Value)
1786 i::HistogramTimerScope execute_timer(isolate->counters()->execute(), true);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001787 i::AggregatingHistogramTimerScope timer(isolate->counters()->compile_lazy());
1788 i::TimerEventScope<i::TimerEventExecute> timer_scope(isolate);
Ben Murdoch097c5b22016-05-18 11:27:45 +01001789 TRACE_EVENT0("v8", "V8.Execute");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001790 auto fun = i::Handle<i::JSFunction>::cast(Utils::OpenHandle(this));
Ben Murdochc5610432016-08-08 18:44:38 +01001791 i::Handle<i::Object> receiver = isolate->global_proxy();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001792 Local<Value> result;
1793 has_pending_exception =
1794 !ToLocal<Value>(i::Execution::Call(isolate, fun, receiver, 0, NULL),
1795 &result);
1796 RETURN_ON_FAILED_EXECUTION(Value);
1797 RETURN_ESCAPED(result);
1798}
1799
1800
Steve Blocka7e24c12009-10-30 11:49:00 +00001801Local<Value> Script::Run() {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001802 auto self = Utils::OpenHandle(this, true);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001803 // If execution is terminating, Compile(..)->Run() requires this
1804 // check.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001805 if (self.is_null()) return Local<Value>();
1806 auto context = ContextFromHeapObject(self);
1807 RETURN_TO_LOCAL_UNCHECKED(Run(context), Value);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001808}
1809
1810
1811Local<UnboundScript> Script::GetUnboundScript() {
1812 i::Handle<i::Object> obj = Utils::OpenHandle(this);
1813 return ToApiHandle<UnboundScript>(
1814 i::Handle<i::SharedFunctionInfo>(i::JSFunction::cast(*obj)->shared()));
1815}
1816
1817
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001818MaybeLocal<UnboundScript> ScriptCompiler::CompileUnboundInternal(
1819 Isolate* v8_isolate, Source* source, CompileOptions options,
1820 bool is_module) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001821 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(v8_isolate);
Ben Murdochc5610432016-08-08 18:44:38 +01001822 PREPARE_FOR_EXECUTION_WITH_ISOLATE(isolate, ScriptCompiler, CompileUnbound,
1823 UnboundScript);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001824
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001825 // Don't try to produce any kind of cache when the debugger is loaded.
1826 if (isolate->debug()->is_loaded() &&
1827 (options == kProduceParserCache || options == kProduceCodeCache)) {
1828 options = kNoCompileOptions;
1829 }
1830
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001831 i::ScriptData* script_data = NULL;
1832 if (options == kConsumeParserCache || options == kConsumeCodeCache) {
1833 DCHECK(source->cached_data);
1834 // ScriptData takes care of pointer-aligning the data.
1835 script_data = new i::ScriptData(source->cached_data->data,
1836 source->cached_data->length);
1837 }
1838
1839 i::Handle<i::String> str = Utils::OpenHandle(*(source->source_string));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001840 i::Handle<i::SharedFunctionInfo> result;
1841 {
1842 i::HistogramTimerScope total(isolate->counters()->compile_script(), true);
Ben Murdoch097c5b22016-05-18 11:27:45 +01001843 TRACE_EVENT0("v8", "V8.CompileScript");
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001844 i::Handle<i::Object> name_obj;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001845 i::Handle<i::Object> source_map_url;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001846 int line_offset = 0;
1847 int column_offset = 0;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001848 if (!source->resource_name.IsEmpty()) {
1849 name_obj = Utils::OpenHandle(*(source->resource_name));
1850 }
1851 if (!source->resource_line_offset.IsEmpty()) {
1852 line_offset = static_cast<int>(source->resource_line_offset->Value());
1853 }
1854 if (!source->resource_column_offset.IsEmpty()) {
1855 column_offset =
1856 static_cast<int>(source->resource_column_offset->Value());
1857 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001858 if (!source->source_map_url.IsEmpty()) {
1859 source_map_url = Utils::OpenHandle(*(source->source_map_url));
Steve Blocka7e24c12009-10-30 11:49:00 +00001860 }
Ben Murdochda12d292016-06-02 14:46:10 +01001861 result = i::Compiler::GetSharedFunctionInfoForScript(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001862 str, name_obj, line_offset, column_offset, source->resource_options,
1863 source_map_url, isolate->native_context(), NULL, &script_data, options,
1864 i::NOT_NATIVES_CODE, is_module);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001865 has_pending_exception = result.is_null();
1866 if (has_pending_exception && script_data != NULL) {
1867 // This case won't happen during normal operation; we have compiled
1868 // successfully and produced cached data, and but the second compilation
1869 // of the same source code fails.
1870 delete script_data;
1871 script_data = NULL;
1872 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001873 RETURN_ON_FAILED_EXECUTION(UnboundScript);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001874
1875 if ((options == kProduceParserCache || options == kProduceCodeCache) &&
1876 script_data != NULL) {
1877 // script_data now contains the data that was generated. source will
1878 // take the ownership.
1879 source->cached_data = new CachedData(
1880 script_data->data(), script_data->length(), CachedData::BufferOwned);
1881 script_data->ReleaseDataOwnership();
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001882 } else if (options == kConsumeParserCache || options == kConsumeCodeCache) {
1883 source->cached_data->rejected = script_data->rejected();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001884 }
1885 delete script_data;
Steve Blocka7e24c12009-10-30 11:49:00 +00001886 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001887 RETURN_ESCAPED(ToApiHandle<UnboundScript>(result));
1888}
1889
1890
1891MaybeLocal<UnboundScript> ScriptCompiler::CompileUnboundScript(
1892 Isolate* v8_isolate, Source* source, CompileOptions options) {
1893 return CompileUnboundInternal(v8_isolate, source, options, false);
1894}
1895
1896
1897Local<UnboundScript> ScriptCompiler::CompileUnbound(Isolate* v8_isolate,
1898 Source* source,
1899 CompileOptions options) {
1900 RETURN_TO_LOCAL_UNCHECKED(
1901 CompileUnboundInternal(v8_isolate, source, options, false),
1902 UnboundScript);
1903}
1904
1905
1906MaybeLocal<Script> ScriptCompiler::Compile(Local<Context> context,
1907 Source* source,
1908 CompileOptions options) {
1909 auto isolate = context->GetIsolate();
1910 auto maybe = CompileUnboundInternal(isolate, source, options, false);
1911 Local<UnboundScript> result;
1912 if (!maybe.ToLocal(&result)) return MaybeLocal<Script>();
1913 v8::Context::Scope scope(context);
1914 return result->BindToCurrentContext();
Steve Blocka7e24c12009-10-30 11:49:00 +00001915}
1916
1917
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001918Local<Script> ScriptCompiler::Compile(
1919 Isolate* v8_isolate,
1920 Source* source,
1921 CompileOptions options) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001922 auto context = v8_isolate->GetCurrentContext();
1923 RETURN_TO_LOCAL_UNCHECKED(Compile(context, source, options), Script);
1924}
1925
1926
1927MaybeLocal<Script> ScriptCompiler::CompileModule(Local<Context> context,
1928 Source* source,
1929 CompileOptions options) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001930 auto isolate = context->GetIsolate();
1931 auto maybe = CompileUnboundInternal(isolate, source, options, true);
1932 Local<UnboundScript> generic;
1933 if (!maybe.ToLocal(&generic)) return MaybeLocal<Script>();
1934 v8::Context::Scope scope(context);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001935 return generic->BindToCurrentContext();
Steve Block6ded16b2010-05-10 14:33:55 +01001936}
1937
1938
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001939class IsIdentifierHelper {
1940 public:
1941 IsIdentifierHelper() : is_identifier_(false), first_char_(true) {}
1942
1943 bool Check(i::String* string) {
1944 i::ConsString* cons_string = i::String::VisitFlat(this, string, 0);
1945 if (cons_string == NULL) return is_identifier_;
1946 // We don't support cons strings here.
1947 return false;
1948 }
1949 void VisitOneByteString(const uint8_t* chars, int length) {
1950 for (int i = 0; i < length; ++i) {
1951 if (first_char_) {
1952 first_char_ = false;
1953 is_identifier_ = unicode_cache_.IsIdentifierStart(chars[0]);
1954 } else {
1955 is_identifier_ &= unicode_cache_.IsIdentifierPart(chars[i]);
1956 }
1957 }
1958 }
1959 void VisitTwoByteString(const uint16_t* chars, int length) {
1960 for (int i = 0; i < length; ++i) {
1961 if (first_char_) {
1962 first_char_ = false;
1963 is_identifier_ = unicode_cache_.IsIdentifierStart(chars[0]);
1964 } else {
1965 is_identifier_ &= unicode_cache_.IsIdentifierPart(chars[i]);
1966 }
1967 }
1968 }
1969
1970 private:
1971 bool is_identifier_;
1972 bool first_char_;
1973 i::UnicodeCache unicode_cache_;
1974 DISALLOW_COPY_AND_ASSIGN(IsIdentifierHelper);
1975};
1976
1977
1978MaybeLocal<Function> ScriptCompiler::CompileFunctionInContext(
1979 Local<Context> v8_context, Source* source, size_t arguments_count,
1980 Local<String> arguments[], size_t context_extension_count,
1981 Local<Object> context_extensions[]) {
Ben Murdochc5610432016-08-08 18:44:38 +01001982 PREPARE_FOR_EXECUTION(v8_context, ScriptCompiler, CompileFunctionInContext,
1983 Function);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001984 i::Handle<i::String> source_string;
1985 auto factory = isolate->factory();
1986 if (arguments_count) {
1987 source_string = factory->NewStringFromStaticChars("(function(");
1988 for (size_t i = 0; i < arguments_count; ++i) {
1989 IsIdentifierHelper helper;
1990 if (!helper.Check(*Utils::OpenHandle(*arguments[i]))) {
1991 return Local<Function>();
1992 }
1993 has_pending_exception =
1994 !factory->NewConsString(source_string,
1995 Utils::OpenHandle(*arguments[i]))
1996 .ToHandle(&source_string);
1997 RETURN_ON_FAILED_EXECUTION(Function);
1998 if (i + 1 == arguments_count) continue;
1999 has_pending_exception =
2000 !factory->NewConsString(source_string,
2001 factory->LookupSingleCharacterStringFromCode(
2002 ',')).ToHandle(&source_string);
2003 RETURN_ON_FAILED_EXECUTION(Function);
2004 }
2005 auto brackets = factory->NewStringFromStaticChars("){");
2006 has_pending_exception = !factory->NewConsString(source_string, brackets)
2007 .ToHandle(&source_string);
2008 RETURN_ON_FAILED_EXECUTION(Function);
2009 } else {
2010 source_string = factory->NewStringFromStaticChars("(function(){");
2011 }
2012
2013 int scope_position = source_string->length();
2014 has_pending_exception =
2015 !factory->NewConsString(source_string,
2016 Utils::OpenHandle(*source->source_string))
2017 .ToHandle(&source_string);
2018 RETURN_ON_FAILED_EXECUTION(Function);
2019 // Include \n in case the source contains a line end comment.
2020 auto brackets = factory->NewStringFromStaticChars("\n})");
2021 has_pending_exception =
2022 !factory->NewConsString(source_string, brackets).ToHandle(&source_string);
2023 RETURN_ON_FAILED_EXECUTION(Function);
2024
2025 i::Handle<i::Context> context = Utils::OpenHandle(*v8_context);
2026 i::Handle<i::SharedFunctionInfo> outer_info(context->closure()->shared(),
2027 isolate);
2028 for (size_t i = 0; i < context_extension_count; ++i) {
2029 i::Handle<i::JSReceiver> extension =
2030 Utils::OpenHandle(*context_extensions[i]);
2031 if (!extension->IsJSObject()) return Local<Function>();
2032 i::Handle<i::JSFunction> closure(context->closure(), isolate);
2033 context = factory->NewWithContext(closure, context, extension);
2034 }
2035
2036 i::Handle<i::Object> name_obj;
Ben Murdochc5610432016-08-08 18:44:38 +01002037 int eval_scope_position = 0;
2038 int eval_position = i::RelocInfo::kNoPosition;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002039 int line_offset = 0;
2040 int column_offset = 0;
2041 if (!source->resource_name.IsEmpty()) {
2042 name_obj = Utils::OpenHandle(*(source->resource_name));
2043 }
2044 if (!source->resource_line_offset.IsEmpty()) {
2045 line_offset = static_cast<int>(source->resource_line_offset->Value());
2046 }
2047 if (!source->resource_column_offset.IsEmpty()) {
2048 column_offset = static_cast<int>(source->resource_column_offset->Value());
2049 }
2050 i::Handle<i::JSFunction> fun;
Ben Murdochc5610432016-08-08 18:44:38 +01002051 has_pending_exception =
2052 !i::Compiler::GetFunctionFromEval(
2053 source_string, outer_info, context, i::SLOPPY,
2054 i::ONLY_SINGLE_FUNCTION_LITERAL, eval_scope_position, eval_position,
2055 line_offset, column_offset - scope_position, name_obj,
2056 source->resource_options)
2057 .ToHandle(&fun);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002058 if (has_pending_exception) {
2059 isolate->ReportPendingMessages();
2060 }
2061 RETURN_ON_FAILED_EXECUTION(Function);
2062
2063 i::Handle<i::Object> result;
2064 has_pending_exception =
2065 !i::Execution::Call(isolate, fun,
2066 Utils::OpenHandle(*v8_context->Global()), 0,
2067 nullptr).ToHandle(&result);
2068 RETURN_ON_FAILED_EXECUTION(Function);
2069 RETURN_ESCAPED(
2070 Utils::CallableToLocal(i::Handle<i::JSFunction>::cast(result)));
2071}
2072
2073
2074Local<Function> ScriptCompiler::CompileFunctionInContext(
2075 Isolate* v8_isolate, Source* source, Local<Context> v8_context,
2076 size_t arguments_count, Local<String> arguments[],
2077 size_t context_extension_count, Local<Object> context_extensions[]) {
2078 RETURN_TO_LOCAL_UNCHECKED(
2079 CompileFunctionInContext(v8_context, source, arguments_count, arguments,
2080 context_extension_count, context_extensions),
2081 Function);
2082}
2083
2084
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002085ScriptCompiler::ScriptStreamingTask* ScriptCompiler::StartStreamingScript(
2086 Isolate* v8_isolate, StreamedSource* source, CompileOptions options) {
2087 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(v8_isolate);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002088 return new i::BackgroundParsingTask(source->impl(), options,
2089 i::FLAG_stack_size, isolate);
2090}
2091
2092
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002093MaybeLocal<Script> ScriptCompiler::Compile(Local<Context> context,
2094 StreamedSource* v8_source,
2095 Local<String> full_source_string,
2096 const ScriptOrigin& origin) {
Ben Murdochc5610432016-08-08 18:44:38 +01002097 PREPARE_FOR_EXECUTION(context, ScriptCompiler, Compile, Script);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002098 i::StreamedSource* source = v8_source->impl();
2099 i::Handle<i::String> str = Utils::OpenHandle(*(full_source_string));
2100 i::Handle<i::Script> script = isolate->factory()->NewScript(str);
2101 if (!origin.ResourceName().IsEmpty()) {
2102 script->set_name(*Utils::OpenHandle(*(origin.ResourceName())));
2103 }
2104 if (!origin.ResourceLineOffset().IsEmpty()) {
2105 script->set_line_offset(
2106 static_cast<int>(origin.ResourceLineOffset()->Value()));
2107 }
2108 if (!origin.ResourceColumnOffset().IsEmpty()) {
2109 script->set_column_offset(
2110 static_cast<int>(origin.ResourceColumnOffset()->Value()));
2111 }
2112 script->set_origin_options(origin.Options());
2113 if (!origin.SourceMapUrl().IsEmpty()) {
2114 script->set_source_mapping_url(
2115 *Utils::OpenHandle(*(origin.SourceMapUrl())));
2116 }
2117
2118 source->info->set_script(script);
2119 source->info->set_context(isolate->native_context());
2120
2121 // Do the parsing tasks which need to be done on the main thread. This will
2122 // also handle parse errors.
2123 source->parser->Internalize(isolate, script,
2124 source->info->literal() == nullptr);
2125 source->parser->HandleSourceURLComments(isolate, script);
2126
2127 i::Handle<i::SharedFunctionInfo> result;
2128 if (source->info->literal() != nullptr) {
2129 // Parsing has succeeded.
Ben Murdochda12d292016-06-02 14:46:10 +01002130 result = i::Compiler::GetSharedFunctionInfoForStreamedScript(
2131 script, source->info.get(), str->length());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002132 }
2133 has_pending_exception = result.is_null();
2134 if (has_pending_exception) isolate->ReportPendingMessages();
2135 RETURN_ON_FAILED_EXECUTION(Script);
2136
2137 source->info->clear_script(); // because script goes out of scope.
2138
2139 Local<UnboundScript> generic = ToApiHandle<UnboundScript>(result);
2140 if (generic.IsEmpty()) return Local<Script>();
2141 Local<Script> bound = generic->BindToCurrentContext();
2142 if (bound.IsEmpty()) return Local<Script>();
2143 RETURN_ESCAPED(bound);
2144}
2145
2146
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002147Local<Script> ScriptCompiler::Compile(Isolate* v8_isolate,
2148 StreamedSource* v8_source,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002149 Local<String> full_source_string,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002150 const ScriptOrigin& origin) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002151 auto context = v8_isolate->GetCurrentContext();
2152 RETURN_TO_LOCAL_UNCHECKED(
2153 Compile(context, v8_source, full_source_string, origin), Script);
Steve Blocka7e24c12009-10-30 11:49:00 +00002154}
2155
2156
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002157uint32_t ScriptCompiler::CachedDataVersionTag() {
2158 return static_cast<uint32_t>(base::hash_combine(
2159 internal::Version::Hash(), internal::FlagList::Hash(),
2160 static_cast<uint32_t>(internal::CpuFeatures::SupportedFeatures())));
2161}
2162
2163
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002164MaybeLocal<Script> Script::Compile(Local<Context> context, Local<String> source,
2165 ScriptOrigin* origin) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002166 if (origin) {
2167 ScriptCompiler::Source script_source(source, *origin);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002168 return ScriptCompiler::Compile(context, &script_source);
Steve Blocka7e24c12009-10-30 11:49:00 +00002169 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002170 ScriptCompiler::Source script_source(source);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002171 return ScriptCompiler::Compile(context, &script_source);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002172}
2173
2174
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002175Local<Script> Script::Compile(v8::Local<String> source,
2176 v8::ScriptOrigin* origin) {
2177 auto str = Utils::OpenHandle(*source);
2178 auto context = ContextFromHeapObject(str);
2179 RETURN_TO_LOCAL_UNCHECKED(Compile(context, source, origin), Script);
2180}
2181
2182
2183Local<Script> Script::Compile(v8::Local<String> source,
2184 v8::Local<String> file_name) {
2185 auto str = Utils::OpenHandle(*source);
2186 auto context = ContextFromHeapObject(str);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002187 ScriptOrigin origin(file_name);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002188 return Compile(context, source, &origin).FromMaybe(Local<Script>());
Steve Blocka7e24c12009-10-30 11:49:00 +00002189}
2190
2191
2192// --- E x c e p t i o n s ---
2193
2194
2195v8::TryCatch::TryCatch()
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002196 : isolate_(i::Isolate::Current()),
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002197 next_(isolate_->try_catch_handler()),
Steve Blocka7e24c12009-10-30 11:49:00 +00002198 is_verbose_(false),
2199 can_continue_(true),
2200 capture_message_(true),
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002201 rethrow_(false),
2202 has_terminated_(false) {
2203 ResetInternal();
2204 // Special handling for simulators which have a separate JS stack.
2205 js_stack_comparable_address_ =
2206 reinterpret_cast<void*>(v8::internal::SimulatorStack::RegisterCTryCatch(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002207 isolate_, v8::internal::GetCurrentStackPosition()));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002208 isolate_->RegisterTryCatchHandler(this);
Steve Blocka7e24c12009-10-30 11:49:00 +00002209}
2210
2211
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002212v8::TryCatch::TryCatch(v8::Isolate* isolate)
2213 : isolate_(reinterpret_cast<i::Isolate*>(isolate)),
2214 next_(isolate_->try_catch_handler()),
2215 is_verbose_(false),
2216 can_continue_(true),
2217 capture_message_(true),
2218 rethrow_(false),
2219 has_terminated_(false) {
2220 ResetInternal();
2221 // Special handling for simulators which have a separate JS stack.
2222 js_stack_comparable_address_ =
2223 reinterpret_cast<void*>(v8::internal::SimulatorStack::RegisterCTryCatch(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002224 isolate_, v8::internal::GetCurrentStackPosition()));
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002225 isolate_->RegisterTryCatchHandler(this);
2226}
2227
2228
Steve Blocka7e24c12009-10-30 11:49:00 +00002229v8::TryCatch::~TryCatch() {
Steve Blockd0582a62009-12-15 09:54:21 +00002230 if (rethrow_) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002231 v8::Isolate* isolate = reinterpret_cast<Isolate*>(isolate_);
2232 v8::HandleScope scope(isolate);
2233 v8::Local<v8::Value> exc = v8::Local<v8::Value>::New(isolate, Exception());
2234 if (HasCaught() && capture_message_) {
2235 // If an exception was caught and rethrow_ is indicated, the saved
2236 // message, script, and location need to be restored to Isolate TLS
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002237 // for reuse. capture_message_ needs to be disabled so that Throw()
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002238 // does not create a new message.
2239 isolate_->thread_local_top()->rethrowing_message_ = true;
2240 isolate_->RestorePendingMessageFromTryCatch(this);
2241 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002242 isolate_->UnregisterTryCatchHandler(this);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002243 v8::internal::SimulatorStack::UnregisterCTryCatch(isolate_);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002244 reinterpret_cast<Isolate*>(isolate_)->ThrowException(exc);
2245 DCHECK(!isolate_->thread_local_top()->rethrowing_message_);
Steve Blockd0582a62009-12-15 09:54:21 +00002246 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002247 if (HasCaught() && isolate_->has_scheduled_exception()) {
2248 // If an exception was caught but is still scheduled because no API call
2249 // promoted it, then it is canceled to prevent it from being propagated.
2250 // Note that this will not cancel termination exceptions.
2251 isolate_->CancelScheduledExceptionFromTryCatch(this);
2252 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002253 isolate_->UnregisterTryCatchHandler(this);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002254 v8::internal::SimulatorStack::UnregisterCTryCatch(isolate_);
Steve Blockd0582a62009-12-15 09:54:21 +00002255 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002256}
2257
2258
2259bool v8::TryCatch::HasCaught() const {
2260 return !reinterpret_cast<i::Object*>(exception_)->IsTheHole();
2261}
2262
2263
2264bool v8::TryCatch::CanContinue() const {
2265 return can_continue_;
2266}
2267
2268
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002269bool v8::TryCatch::HasTerminated() const {
2270 return has_terminated_;
2271}
2272
2273
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002274v8::Local<v8::Value> v8::TryCatch::ReThrow() {
Steve Blockd0582a62009-12-15 09:54:21 +00002275 if (!HasCaught()) return v8::Local<v8::Value>();
2276 rethrow_ = true;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002277 return v8::Undefined(reinterpret_cast<v8::Isolate*>(isolate_));
Steve Blockd0582a62009-12-15 09:54:21 +00002278}
2279
2280
Steve Blocka7e24c12009-10-30 11:49:00 +00002281v8::Local<Value> v8::TryCatch::Exception() const {
2282 if (HasCaught()) {
2283 // Check for out of memory exception.
2284 i::Object* exception = reinterpret_cast<i::Object*>(exception_);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002285 return v8::Utils::ToLocal(i::Handle<i::Object>(exception, isolate_));
Steve Blocka7e24c12009-10-30 11:49:00 +00002286 } else {
2287 return v8::Local<Value>();
2288 }
2289}
2290
2291
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002292MaybeLocal<Value> v8::TryCatch::StackTrace(Local<Context> context) const {
2293 if (!HasCaught()) return v8::Local<Value>();
2294 i::Object* raw_obj = reinterpret_cast<i::Object*>(exception_);
2295 if (!raw_obj->IsJSObject()) return v8::Local<Value>();
Ben Murdochc5610432016-08-08 18:44:38 +01002296 PREPARE_FOR_EXECUTION(context, TryCatch, StackTrace, Value);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002297 i::Handle<i::JSObject> obj(i::JSObject::cast(raw_obj), isolate_);
2298 i::Handle<i::String> name = isolate->factory()->stack_string();
2299 Maybe<bool> maybe = i::JSReceiver::HasProperty(obj, name);
2300 has_pending_exception = !maybe.IsJust();
2301 RETURN_ON_FAILED_EXECUTION(Value);
2302 if (!maybe.FromJust()) return v8::Local<Value>();
2303 Local<Value> result;
2304 has_pending_exception =
Ben Murdochda12d292016-06-02 14:46:10 +01002305 !ToLocal<Value>(i::JSReceiver::GetProperty(obj, name), &result);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002306 RETURN_ON_FAILED_EXECUTION(Value);
2307 RETURN_ESCAPED(result);
2308}
2309
2310
Steve Blocka7e24c12009-10-30 11:49:00 +00002311v8::Local<Value> v8::TryCatch::StackTrace() const {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002312 auto context = reinterpret_cast<v8::Isolate*>(isolate_)->GetCurrentContext();
2313 RETURN_TO_LOCAL_UNCHECKED(StackTrace(context), Value);
Steve Blocka7e24c12009-10-30 11:49:00 +00002314}
2315
2316
2317v8::Local<v8::Message> v8::TryCatch::Message() const {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002318 i::Object* message = reinterpret_cast<i::Object*>(message_obj_);
2319 DCHECK(message->IsJSMessageObject() || message->IsTheHole());
2320 if (HasCaught() && !message->IsTheHole()) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002321 return v8::Utils::MessageToLocal(i::Handle<i::Object>(message, isolate_));
Steve Blocka7e24c12009-10-30 11:49:00 +00002322 } else {
2323 return v8::Local<v8::Message>();
2324 }
2325}
2326
2327
2328void v8::TryCatch::Reset() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002329 if (!rethrow_ && HasCaught() && isolate_->has_scheduled_exception()) {
2330 // If an exception was caught but is still scheduled because no API call
2331 // promoted it, then it is canceled to prevent it from being propagated.
2332 // Note that this will not cancel termination exceptions.
2333 isolate_->CancelScheduledExceptionFromTryCatch(this);
2334 }
2335 ResetInternal();
2336}
2337
2338
2339void v8::TryCatch::ResetInternal() {
2340 i::Object* the_hole = isolate_->heap()->the_hole_value();
2341 exception_ = the_hole;
2342 message_obj_ = the_hole;
Steve Blocka7e24c12009-10-30 11:49:00 +00002343}
2344
2345
2346void v8::TryCatch::SetVerbose(bool value) {
2347 is_verbose_ = value;
2348}
2349
2350
2351void v8::TryCatch::SetCaptureMessage(bool value) {
2352 capture_message_ = value;
2353}
2354
2355
2356// --- M e s s a g e ---
2357
2358
2359Local<String> Message::Get() const {
Steve Block44f0eee2011-05-26 01:26:41 +01002360 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
Steve Block44f0eee2011-05-26 01:26:41 +01002361 ENTER_V8(isolate);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002362 EscapableHandleScope scope(reinterpret_cast<Isolate*>(isolate));
Steve Blocka7e24c12009-10-30 11:49:00 +00002363 i::Handle<i::Object> obj = Utils::OpenHandle(this);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002364 i::Handle<i::String> raw_result = i::MessageHandler::GetMessage(isolate, obj);
Steve Blocka7e24c12009-10-30 11:49:00 +00002365 Local<String> result = Utils::ToLocal(raw_result);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002366 return scope.Escape(result);
2367}
2368
2369
2370ScriptOrigin Message::GetScriptOrigin() const {
2371 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002372 auto message = i::Handle<i::JSMessageObject>::cast(Utils::OpenHandle(this));
2373 auto script_wraper = i::Handle<i::Object>(message->script(), isolate);
2374 auto script_value = i::Handle<i::JSValue>::cast(script_wraper);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002375 i::Handle<i::Script> script(i::Script::cast(script_value->value()));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002376 return GetScriptOriginForScript(isolate, script);
Steve Blocka7e24c12009-10-30 11:49:00 +00002377}
2378
2379
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002380v8::Local<Value> Message::GetScriptResourceName() const {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002381 return GetScriptOrigin().ResourceName();
Steve Blocka7e24c12009-10-30 11:49:00 +00002382}
2383
2384
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002385v8::Local<v8::StackTrace> Message::GetStackTrace() const {
Steve Block44f0eee2011-05-26 01:26:41 +01002386 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
Steve Block44f0eee2011-05-26 01:26:41 +01002387 ENTER_V8(isolate);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002388 EscapableHandleScope scope(reinterpret_cast<Isolate*>(isolate));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002389 auto message = i::Handle<i::JSMessageObject>::cast(Utils::OpenHandle(this));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002390 i::Handle<i::Object> stackFramesObj(message->stack_frames(), isolate);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002391 if (!stackFramesObj->IsJSArray()) return v8::Local<v8::StackTrace>();
2392 auto stackTrace = i::Handle<i::JSArray>::cast(stackFramesObj);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002393 return scope.Escape(Utils::StackTraceToLocal(stackTrace));
Ben Murdoch3bec4d22010-07-22 14:51:16 +01002394}
2395
2396
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002397Maybe<int> Message::GetLineNumber(Local<Context> context) const {
Ben Murdochc5610432016-08-08 18:44:38 +01002398 PREPARE_FOR_EXECUTION_PRIMITIVE(context, Message, GetLineNumber, int);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002399 i::Handle<i::JSFunction> fun = isolate->message_get_line_number();
2400 i::Handle<i::Object> undefined = isolate->factory()->undefined_value();
2401 i::Handle<i::Object> args[] = {Utils::OpenHandle(this)};
2402 i::Handle<i::Object> result;
2403 has_pending_exception =
2404 !i::Execution::Call(isolate, fun, undefined, arraysize(args), args)
2405 .ToHandle(&result);
2406 RETURN_ON_FAILED_EXECUTION_PRIMITIVE(int);
2407 return Just(static_cast<int>(result->Number()));
Steve Blocka7e24c12009-10-30 11:49:00 +00002408}
2409
2410
2411int Message::GetLineNumber() const {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002412 auto context = ContextFromHeapObject(Utils::OpenHandle(this));
2413 return GetLineNumber(context).FromMaybe(0);
Steve Blocka7e24c12009-10-30 11:49:00 +00002414}
2415
2416
2417int Message::GetStartPosition() const {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002418 auto self = Utils::OpenHandle(this);
2419 return self->start_position();
Steve Blocka7e24c12009-10-30 11:49:00 +00002420}
2421
2422
2423int Message::GetEndPosition() const {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002424 auto self = Utils::OpenHandle(this);
2425 return self->end_position();
2426}
2427
2428
2429Maybe<int> Message::GetStartColumn(Local<Context> context) const {
Ben Murdochc5610432016-08-08 18:44:38 +01002430 PREPARE_FOR_EXECUTION_PRIMITIVE(context, Message, GetStartColumn, int);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002431 i::Handle<i::JSFunction> fun = isolate->message_get_column_number();
2432 i::Handle<i::Object> undefined = isolate->factory()->undefined_value();
2433 i::Handle<i::Object> args[] = {Utils::OpenHandle(this)};
2434 i::Handle<i::Object> result;
2435 has_pending_exception =
2436 !i::Execution::Call(isolate, fun, undefined, arraysize(args), args)
2437 .ToHandle(&result);
2438 RETURN_ON_FAILED_EXECUTION_PRIMITIVE(int);
2439 return Just(static_cast<int>(result->Number()));
Steve Blocka7e24c12009-10-30 11:49:00 +00002440}
2441
2442
2443int Message::GetStartColumn() const {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002444 auto context = ContextFromHeapObject(Utils::OpenHandle(this));
2445 const int default_value = kNoColumnInfo;
2446 return GetStartColumn(context).FromMaybe(default_value);
2447}
2448
2449
2450Maybe<int> Message::GetEndColumn(Local<Context> context) const {
2451 auto self = Utils::OpenHandle(this);
Ben Murdochc5610432016-08-08 18:44:38 +01002452 PREPARE_FOR_EXECUTION_PRIMITIVE(context, Message, GetEndColumn, int);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002453 i::Handle<i::JSFunction> fun = isolate->message_get_column_number();
2454 i::Handle<i::Object> undefined = isolate->factory()->undefined_value();
2455 i::Handle<i::Object> args[] = {self};
2456 i::Handle<i::Object> result;
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002457 has_pending_exception =
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002458 !i::Execution::Call(isolate, fun, undefined, arraysize(args), args)
2459 .ToHandle(&result);
2460 RETURN_ON_FAILED_EXECUTION_PRIMITIVE(int);
2461 int start = self->start_position();
2462 int end = self->end_position();
2463 return Just(static_cast<int>(result->Number()) + (end - start));
Steve Blocka7e24c12009-10-30 11:49:00 +00002464}
2465
2466
2467int Message::GetEndColumn() const {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002468 auto context = ContextFromHeapObject(Utils::OpenHandle(this));
2469 const int default_value = kNoColumnInfo;
2470 return GetEndColumn(context).FromMaybe(default_value);
Steve Blocka7e24c12009-10-30 11:49:00 +00002471}
2472
2473
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002474bool Message::IsSharedCrossOrigin() const {
2475 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
2476 ENTER_V8(isolate);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002477 auto self = Utils::OpenHandle(this);
2478 auto script = i::Handle<i::JSValue>::cast(
2479 i::Handle<i::Object>(self->script(), isolate));
2480 return i::Script::cast(script->value())
2481 ->origin_options()
2482 .IsSharedCrossOrigin();
2483}
2484
2485bool Message::IsOpaque() const {
2486 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
2487 ENTER_V8(isolate);
2488 auto self = Utils::OpenHandle(this);
2489 auto script = i::Handle<i::JSValue>::cast(
2490 i::Handle<i::Object>(self->script(), isolate));
2491 return i::Script::cast(script->value())->origin_options().IsOpaque();
2492}
2493
2494
2495MaybeLocal<String> Message::GetSourceLine(Local<Context> context) const {
Ben Murdochc5610432016-08-08 18:44:38 +01002496 PREPARE_FOR_EXECUTION(context, Message, GetSourceLine, String);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002497 i::Handle<i::JSFunction> fun = isolate->message_get_source_line();
2498 i::Handle<i::Object> undefined = isolate->factory()->undefined_value();
2499 i::Handle<i::Object> args[] = {Utils::OpenHandle(this)};
2500 i::Handle<i::Object> result;
2501 has_pending_exception =
2502 !i::Execution::Call(isolate, fun, undefined, arraysize(args), args)
2503 .ToHandle(&result);
2504 RETURN_ON_FAILED_EXECUTION(String);
2505 Local<String> str;
2506 if (result->IsString()) {
2507 str = Utils::ToLocal(i::Handle<i::String>::cast(result));
2508 }
2509 RETURN_ESCAPED(str);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002510}
2511
2512
Steve Blocka7e24c12009-10-30 11:49:00 +00002513Local<String> Message::GetSourceLine() const {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002514 auto context = ContextFromHeapObject(Utils::OpenHandle(this));
2515 RETURN_TO_LOCAL_UNCHECKED(GetSourceLine(context), String)
Steve Blocka7e24c12009-10-30 11:49:00 +00002516}
2517
2518
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002519void Message::PrintCurrentStackTrace(Isolate* isolate, FILE* out) {
2520 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
2521 ENTER_V8(i_isolate);
2522 i_isolate->PrintCurrentStackTrace(out);
Steve Blocka7e24c12009-10-30 11:49:00 +00002523}
2524
2525
Kristian Monsen25f61362010-05-21 11:50:48 +01002526// --- S t a c k T r a c e ---
2527
2528Local<StackFrame> StackTrace::GetFrame(uint32_t index) const {
Steve Block44f0eee2011-05-26 01:26:41 +01002529 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
Steve Block44f0eee2011-05-26 01:26:41 +01002530 ENTER_V8(isolate);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002531 EscapableHandleScope scope(reinterpret_cast<Isolate*>(isolate));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002532 auto self = Utils::OpenHandle(this);
Ben Murdochda12d292016-06-02 14:46:10 +01002533 auto obj = i::JSReceiver::GetElement(isolate, self, index).ToHandleChecked();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002534 auto jsobj = i::Handle<i::JSObject>::cast(obj);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002535 return scope.Escape(Utils::StackFrameToLocal(jsobj));
Kristian Monsen25f61362010-05-21 11:50:48 +01002536}
2537
2538
2539int StackTrace::GetFrameCount() const {
Kristian Monsen25f61362010-05-21 11:50:48 +01002540 return i::Smi::cast(Utils::OpenHandle(this)->length())->value();
2541}
2542
2543
2544Local<Array> StackTrace::AsArray() {
Kristian Monsen25f61362010-05-21 11:50:48 +01002545 return Utils::ToLocal(Utils::OpenHandle(this));
2546}
2547
2548
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002549Local<StackTrace> StackTrace::CurrentStackTrace(
2550 Isolate* isolate,
2551 int frame_limit,
Kristian Monsen25f61362010-05-21 11:50:48 +01002552 StackTraceOptions options) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002553 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
2554 ENTER_V8(i_isolate);
2555 // TODO(dcarney): remove when ScriptDebugServer is fixed.
2556 options = static_cast<StackTraceOptions>(
2557 static_cast<int>(options) | kExposeFramesAcrossSecurityOrigins);
Ben Murdoch3bec4d22010-07-22 14:51:16 +01002558 i::Handle<i::JSArray> stackTrace =
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002559 i_isolate->CaptureCurrentStackTrace(frame_limit, options);
Ben Murdoch3bec4d22010-07-22 14:51:16 +01002560 return Utils::StackTraceToLocal(stackTrace);
Kristian Monsen25f61362010-05-21 11:50:48 +01002561}
2562
2563
2564// --- S t a c k F r a m e ---
2565
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002566static int getIntProperty(const StackFrame* f, const char* propertyName,
2567 int defaultValue) {
2568 i::Isolate* isolate = Utils::OpenHandle(f)->GetIsolate();
Steve Block44f0eee2011-05-26 01:26:41 +01002569 ENTER_V8(isolate);
2570 i::HandleScope scope(isolate);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002571 i::Handle<i::JSObject> self = Utils::OpenHandle(f);
2572 i::Handle<i::Object> obj =
Ben Murdochda12d292016-06-02 14:46:10 +01002573 i::JSReceiver::GetProperty(isolate, self, propertyName).ToHandleChecked();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002574 return obj->IsSmi() ? i::Smi::cast(*obj)->value() : defaultValue;
2575}
2576
2577
2578int StackFrame::GetLineNumber() const {
2579 return getIntProperty(this, "lineNumber", Message::kNoLineNumberInfo);
Kristian Monsen25f61362010-05-21 11:50:48 +01002580}
2581
2582
2583int StackFrame::GetColumn() const {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002584 return getIntProperty(this, "column", Message::kNoColumnInfo);
2585}
2586
2587
2588int StackFrame::GetScriptId() const {
2589 return getIntProperty(this, "scriptId", Message::kNoScriptIdInfo);
2590}
2591
2592
2593static Local<String> getStringProperty(const StackFrame* f,
2594 const char* propertyName) {
2595 i::Isolate* isolate = Utils::OpenHandle(f)->GetIsolate();
Steve Block44f0eee2011-05-26 01:26:41 +01002596 ENTER_V8(isolate);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002597 EscapableHandleScope scope(reinterpret_cast<Isolate*>(isolate));
2598 i::Handle<i::JSObject> self = Utils::OpenHandle(f);
2599 i::Handle<i::Object> obj =
Ben Murdochda12d292016-06-02 14:46:10 +01002600 i::JSReceiver::GetProperty(isolate, self, propertyName).ToHandleChecked();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002601 return obj->IsString()
2602 ? scope.Escape(Local<String>::Cast(Utils::ToLocal(obj)))
2603 : Local<String>();
Kristian Monsen25f61362010-05-21 11:50:48 +01002604}
2605
2606
2607Local<String> StackFrame::GetScriptName() const {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002608 return getStringProperty(this, "scriptName");
Kristian Monsen25f61362010-05-21 11:50:48 +01002609}
2610
2611
Ben Murdochf87a2032010-10-22 12:50:53 +01002612Local<String> StackFrame::GetScriptNameOrSourceURL() const {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002613 return getStringProperty(this, "scriptNameOrSourceURL");
Ben Murdochf87a2032010-10-22 12:50:53 +01002614}
2615
2616
Kristian Monsen25f61362010-05-21 11:50:48 +01002617Local<String> StackFrame::GetFunctionName() const {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002618 return getStringProperty(this, "functionName");
Kristian Monsen25f61362010-05-21 11:50:48 +01002619}
2620
2621
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002622static bool getBoolProperty(const StackFrame* f, const char* propertyName) {
2623 i::Isolate* isolate = Utils::OpenHandle(f)->GetIsolate();
Steve Block44f0eee2011-05-26 01:26:41 +01002624 ENTER_V8(isolate);
2625 i::HandleScope scope(isolate);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002626 i::Handle<i::JSObject> self = Utils::OpenHandle(f);
2627 i::Handle<i::Object> obj =
Ben Murdochda12d292016-06-02 14:46:10 +01002628 i::JSReceiver::GetProperty(isolate, self, propertyName).ToHandleChecked();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002629 return obj->IsTrue();
Kristian Monsen25f61362010-05-21 11:50:48 +01002630}
2631
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002632bool StackFrame::IsEval() const { return getBoolProperty(this, "isEval"); }
2633
Kristian Monsen25f61362010-05-21 11:50:48 +01002634
2635bool StackFrame::IsConstructor() const {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002636 return getBoolProperty(this, "isConstructor");
2637}
2638
2639
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002640// --- N a t i v e W e a k M a p ---
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002641
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002642Local<NativeWeakMap> NativeWeakMap::New(Isolate* v8_isolate) {
2643 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(v8_isolate);
2644 ENTER_V8(isolate);
2645 i::Handle<i::JSWeakMap> weakmap = isolate->factory()->NewJSWeakMap();
2646 i::JSWeakCollection::Initialize(weakmap, isolate);
2647 return Utils::NativeWeakMapToLocal(weakmap);
2648}
2649
2650
2651void NativeWeakMap::Set(Local<Value> v8_key, Local<Value> v8_value) {
2652 i::Handle<i::JSWeakMap> weak_collection = Utils::OpenHandle(this);
2653 i::Isolate* isolate = weak_collection->GetIsolate();
Steve Block44f0eee2011-05-26 01:26:41 +01002654 ENTER_V8(isolate);
2655 i::HandleScope scope(isolate);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002656 i::Handle<i::Object> key = Utils::OpenHandle(*v8_key);
2657 i::Handle<i::Object> value = Utils::OpenHandle(*v8_value);
2658 if (!key->IsJSReceiver() && !key->IsSymbol()) {
2659 DCHECK(false);
2660 return;
2661 }
2662 i::Handle<i::ObjectHashTable> table(
2663 i::ObjectHashTable::cast(weak_collection->table()));
2664 if (!table->IsKey(*key)) {
2665 DCHECK(false);
2666 return;
2667 }
2668 int32_t hash = i::Object::GetOrCreateHash(isolate, key)->value();
2669 i::JSWeakCollection::Set(weak_collection, key, value, hash);
2670}
2671
2672
2673Local<Value> NativeWeakMap::Get(Local<Value> v8_key) {
2674 i::Handle<i::JSWeakMap> weak_collection = Utils::OpenHandle(this);
2675 i::Isolate* isolate = weak_collection->GetIsolate();
2676 ENTER_V8(isolate);
2677 i::Handle<i::Object> key = Utils::OpenHandle(*v8_key);
2678 if (!key->IsJSReceiver() && !key->IsSymbol()) {
2679 DCHECK(false);
2680 return v8::Undefined(reinterpret_cast<v8::Isolate*>(isolate));
2681 }
2682 i::Handle<i::ObjectHashTable> table(
2683 i::ObjectHashTable::cast(weak_collection->table()));
2684 if (!table->IsKey(*key)) {
2685 DCHECK(false);
2686 return v8::Undefined(reinterpret_cast<v8::Isolate*>(isolate));
2687 }
2688 i::Handle<i::Object> lookup(table->Lookup(key), isolate);
2689 if (lookup->IsTheHole())
2690 return v8::Undefined(reinterpret_cast<v8::Isolate*>(isolate));
2691 return Utils::ToLocal(lookup);
2692}
2693
2694
2695bool NativeWeakMap::Has(Local<Value> v8_key) {
2696 i::Handle<i::JSWeakMap> weak_collection = Utils::OpenHandle(this);
2697 i::Isolate* isolate = weak_collection->GetIsolate();
2698 ENTER_V8(isolate);
2699 i::HandleScope scope(isolate);
2700 i::Handle<i::Object> key = Utils::OpenHandle(*v8_key);
2701 if (!key->IsJSReceiver() && !key->IsSymbol()) {
2702 DCHECK(false);
2703 return false;
2704 }
2705 i::Handle<i::ObjectHashTable> table(
2706 i::ObjectHashTable::cast(weak_collection->table()));
2707 if (!table->IsKey(*key)) {
2708 DCHECK(false);
2709 return false;
2710 }
2711 i::Handle<i::Object> lookup(table->Lookup(key), isolate);
2712 return !lookup->IsTheHole();
2713}
2714
2715
2716bool NativeWeakMap::Delete(Local<Value> v8_key) {
2717 i::Handle<i::JSWeakMap> weak_collection = Utils::OpenHandle(this);
2718 i::Isolate* isolate = weak_collection->GetIsolate();
2719 ENTER_V8(isolate);
2720 i::HandleScope scope(isolate);
2721 i::Handle<i::Object> key = Utils::OpenHandle(*v8_key);
2722 if (!key->IsJSReceiver() && !key->IsSymbol()) {
2723 DCHECK(false);
2724 return false;
2725 }
2726 i::Handle<i::ObjectHashTable> table(
2727 i::ObjectHashTable::cast(weak_collection->table()));
2728 if (!table->IsKey(*key)) {
2729 DCHECK(false);
2730 return false;
2731 }
2732 int32_t hash = i::Object::GetOrCreateHash(isolate, key)->value();
2733 return i::JSWeakCollection::Delete(weak_collection, key, hash);
2734}
2735
2736
2737// --- J S O N ---
2738
2739MaybeLocal<Value> JSON::Parse(Isolate* v8_isolate, Local<String> json_string) {
2740 auto isolate = reinterpret_cast<i::Isolate*>(v8_isolate);
Ben Murdochc5610432016-08-08 18:44:38 +01002741 PREPARE_FOR_EXECUTION_WITH_ISOLATE(isolate, JSON, Parse, Value);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002742 i::Handle<i::String> string = Utils::OpenHandle(*json_string);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002743 i::Handle<i::String> source = i::String::Flatten(string);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002744 auto maybe = source->IsSeqOneByteString()
2745 ? i::JsonParser<true>::Parse(source)
2746 : i::JsonParser<false>::Parse(source);
2747 Local<Value> result;
2748 has_pending_exception = !ToLocal<Value>(maybe, &result);
2749 RETURN_ON_FAILED_EXECUTION(Value);
2750 RETURN_ESCAPED(result);
2751}
2752
Ben Murdochc5610432016-08-08 18:44:38 +01002753MaybeLocal<Value> JSON::Parse(Local<Context> context,
2754 Local<String> json_string) {
2755 PREPARE_FOR_EXECUTION(context, JSON, Parse, Value);
2756 i::Handle<i::String> string = Utils::OpenHandle(*json_string);
2757 i::Handle<i::String> source = i::String::Flatten(string);
2758 auto maybe = source->IsSeqOneByteString()
2759 ? i::JsonParser<true>::Parse(source)
2760 : i::JsonParser<false>::Parse(source);
2761 Local<Value> result;
2762 has_pending_exception = !ToLocal<Value>(maybe, &result);
2763 RETURN_ON_FAILED_EXECUTION(Value);
2764 RETURN_ESCAPED(result);
Kristian Monsen25f61362010-05-21 11:50:48 +01002765}
2766
Ben Murdochc5610432016-08-08 18:44:38 +01002767Local<Value> JSON::Parse(Local<String> json_string) {
2768 RETURN_TO_LOCAL_UNCHECKED(Parse(Local<Context>(), json_string), Value);
2769}
2770
2771MaybeLocal<String> JSON::Stringify(Local<Context> context,
2772 Local<Object> json_object) {
2773 PREPARE_FOR_EXECUTION(context, JSON, Stringify, String);
2774 i::Handle<i::Object> object = Utils::OpenHandle(*json_object);
2775 i::Handle<i::Object> maybe;
2776 has_pending_exception =
2777 !i::Runtime::BasicJsonStringify(isolate, object).ToHandle(&maybe);
2778 RETURN_ON_FAILED_EXECUTION(String);
2779 Local<String> result;
2780 has_pending_exception =
2781 !ToLocal<String>(i::Object::ToString(isolate, maybe), &result);
2782 RETURN_ON_FAILED_EXECUTION(String);
2783 RETURN_ESCAPED(result);
2784}
Kristian Monsen25f61362010-05-21 11:50:48 +01002785
Steve Blocka7e24c12009-10-30 11:49:00 +00002786// --- D a t a ---
2787
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002788bool Value::FullIsUndefined() const {
2789 bool result = Utils::OpenHandle(this)->IsUndefined();
2790 DCHECK_EQ(result, QuickIsUndefined());
2791 return result;
Steve Blocka7e24c12009-10-30 11:49:00 +00002792}
2793
2794
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002795bool Value::FullIsNull() const {
2796 bool result = Utils::OpenHandle(this)->IsNull();
2797 DCHECK_EQ(result, QuickIsNull());
2798 return result;
Steve Blocka7e24c12009-10-30 11:49:00 +00002799}
2800
2801
2802bool Value::IsTrue() const {
Steve Blocka7e24c12009-10-30 11:49:00 +00002803 return Utils::OpenHandle(this)->IsTrue();
2804}
2805
2806
2807bool Value::IsFalse() const {
Steve Blocka7e24c12009-10-30 11:49:00 +00002808 return Utils::OpenHandle(this)->IsFalse();
2809}
2810
2811
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002812bool Value::IsFunction() const { return Utils::OpenHandle(this)->IsCallable(); }
Steve Blocka7e24c12009-10-30 11:49:00 +00002813
2814
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002815bool Value::IsName() const {
2816 return Utils::OpenHandle(this)->IsName();
2817}
2818
2819
Steve Blocka7e24c12009-10-30 11:49:00 +00002820bool Value::FullIsString() const {
Steve Blocka7e24c12009-10-30 11:49:00 +00002821 bool result = Utils::OpenHandle(this)->IsString();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002822 DCHECK_EQ(result, QuickIsString());
Steve Blocka7e24c12009-10-30 11:49:00 +00002823 return result;
2824}
2825
2826
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002827bool Value::IsSymbol() const {
2828 return Utils::OpenHandle(this)->IsSymbol();
2829}
2830
2831
Steve Blocka7e24c12009-10-30 11:49:00 +00002832bool Value::IsArray() const {
Steve Blocka7e24c12009-10-30 11:49:00 +00002833 return Utils::OpenHandle(this)->IsJSArray();
2834}
2835
2836
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002837bool Value::IsArrayBuffer() const {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002838 i::Handle<i::Object> obj = Utils::OpenHandle(this);
2839 return obj->IsJSArrayBuffer() && !i::JSArrayBuffer::cast(*obj)->is_shared();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002840}
2841
2842
2843bool Value::IsArrayBufferView() const {
2844 return Utils::OpenHandle(this)->IsJSArrayBufferView();
2845}
2846
2847
2848bool Value::IsTypedArray() const {
2849 return Utils::OpenHandle(this)->IsJSTypedArray();
2850}
2851
2852
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002853#define VALUE_IS_TYPED_ARRAY(Type, typeName, TYPE, ctype, size) \
2854 bool Value::Is##Type##Array() const { \
2855 i::Handle<i::Object> obj = Utils::OpenHandle(this); \
2856 return obj->IsJSTypedArray() && \
2857 i::JSTypedArray::cast(*obj)->type() == i::kExternal##Type##Array; \
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002858 }
2859
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002860
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002861TYPED_ARRAYS(VALUE_IS_TYPED_ARRAY)
2862
2863#undef VALUE_IS_TYPED_ARRAY
2864
2865
2866bool Value::IsDataView() const {
2867 return Utils::OpenHandle(this)->IsJSDataView();
2868}
2869
2870
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002871bool Value::IsSharedArrayBuffer() const {
2872 i::Handle<i::Object> obj = Utils::OpenHandle(this);
2873 return obj->IsJSArrayBuffer() && i::JSArrayBuffer::cast(*obj)->is_shared();
Steve Blocka7e24c12009-10-30 11:49:00 +00002874}
2875
2876
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002877bool Value::IsObject() const { return Utils::OpenHandle(this)->IsJSReceiver(); }
2878
2879
Steve Blocka7e24c12009-10-30 11:49:00 +00002880bool Value::IsNumber() const {
Steve Blocka7e24c12009-10-30 11:49:00 +00002881 return Utils::OpenHandle(this)->IsNumber();
2882}
2883
2884
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002885bool Value::IsProxy() const { return Utils::OpenHandle(this)->IsJSProxy(); }
2886
2887
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002888#define VALUE_IS_SPECIFIC_TYPE(Type, Class) \
2889 bool Value::Is##Type() const { \
2890 i::Handle<i::Object> obj = Utils::OpenHandle(this); \
2891 if (!obj->IsHeapObject()) return false; \
2892 i::Isolate* isolate = i::HeapObject::cast(*obj)->GetIsolate(); \
2893 return obj->HasSpecificClassOf(isolate->heap()->Class##_string()); \
Steve Block44f0eee2011-05-26 01:26:41 +01002894 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002895
2896VALUE_IS_SPECIFIC_TYPE(ArgumentsObject, Arguments)
2897VALUE_IS_SPECIFIC_TYPE(BooleanObject, Boolean)
2898VALUE_IS_SPECIFIC_TYPE(NumberObject, Number)
2899VALUE_IS_SPECIFIC_TYPE(StringObject, String)
2900VALUE_IS_SPECIFIC_TYPE(SymbolObject, Symbol)
2901VALUE_IS_SPECIFIC_TYPE(Date, Date)
2902VALUE_IS_SPECIFIC_TYPE(Map, Map)
2903VALUE_IS_SPECIFIC_TYPE(Set, Set)
2904VALUE_IS_SPECIFIC_TYPE(WeakMap, WeakMap)
2905VALUE_IS_SPECIFIC_TYPE(WeakSet, WeakSet)
2906
2907#undef VALUE_IS_SPECIFIC_TYPE
2908
2909
2910bool Value::IsBoolean() const {
Steve Blocka7e24c12009-10-30 11:49:00 +00002911 return Utils::OpenHandle(this)->IsBoolean();
2912}
2913
2914
2915bool Value::IsExternal() const {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002916 return Utils::OpenHandle(this)->IsExternal();
Steve Blocka7e24c12009-10-30 11:49:00 +00002917}
2918
2919
2920bool Value::IsInt32() const {
Steve Blocka7e24c12009-10-30 11:49:00 +00002921 i::Handle<i::Object> obj = Utils::OpenHandle(this);
2922 if (obj->IsSmi()) return true;
2923 if (obj->IsNumber()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002924 return i::IsInt32Double(obj->Number());
Steve Blocka7e24c12009-10-30 11:49:00 +00002925 }
2926 return false;
2927}
2928
2929
Steve Block6ded16b2010-05-10 14:33:55 +01002930bool Value::IsUint32() const {
Steve Block6ded16b2010-05-10 14:33:55 +01002931 i::Handle<i::Object> obj = Utils::OpenHandle(this);
2932 if (obj->IsSmi()) return i::Smi::cast(*obj)->value() >= 0;
2933 if (obj->IsNumber()) {
2934 double value = obj->Number();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002935 return !i::IsMinusZero(value) &&
2936 value >= 0 &&
2937 value <= i::kMaxUInt32 &&
2938 value == i::FastUI2D(i::FastD2UI(value));
Steve Block6ded16b2010-05-10 14:33:55 +01002939 }
2940 return false;
2941}
2942
2943
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002944bool Value::IsNativeError() const {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002945 i::Handle<i::Object> obj = Utils::OpenHandle(this);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002946 if (!obj->IsJSObject()) return false;
2947 i::Handle<i::JSObject> js_obj = i::Handle<i::JSObject>::cast(obj);
2948 i::Isolate* isolate = js_obj->GetIsolate();
2949 i::Handle<i::Object> constructor(js_obj->map()->GetConstructor(), isolate);
2950 if (!constructor->IsJSFunction()) return false;
2951 i::Handle<i::JSFunction> function =
2952 i::Handle<i::JSFunction>::cast(constructor);
2953 if (!function->shared()->native()) return false;
2954 return function.is_identical_to(isolate->error_function()) ||
2955 function.is_identical_to(isolate->eval_error_function()) ||
2956 function.is_identical_to(isolate->range_error_function()) ||
2957 function.is_identical_to(isolate->reference_error_function()) ||
2958 function.is_identical_to(isolate->syntax_error_function()) ||
2959 function.is_identical_to(isolate->type_error_function()) ||
2960 function.is_identical_to(isolate->uri_error_function());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002961}
2962
2963
Iain Merrick75681382010-08-19 15:07:18 +01002964bool Value::IsRegExp() const {
Iain Merrick75681382010-08-19 15:07:18 +01002965 i::Handle<i::Object> obj = Utils::OpenHandle(this);
2966 return obj->IsJSRegExp();
2967}
2968
2969
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002970bool Value::IsGeneratorFunction() const {
2971 i::Handle<i::Object> obj = Utils::OpenHandle(this);
2972 if (!obj->IsJSFunction()) return false;
2973 i::Handle<i::JSFunction> func = i::Handle<i::JSFunction>::cast(obj);
2974 return func->shared()->is_generator();
2975}
2976
2977
2978bool Value::IsGeneratorObject() const {
2979 return Utils::OpenHandle(this)->IsJSGeneratorObject();
2980}
2981
2982
2983bool Value::IsMapIterator() const {
2984 return Utils::OpenHandle(this)->IsJSMapIterator();
2985}
2986
2987
2988bool Value::IsSetIterator() const {
2989 return Utils::OpenHandle(this)->IsJSSetIterator();
2990}
2991
2992
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002993bool Value::IsPromise() const {
2994 auto self = Utils::OpenHandle(this);
2995 return i::Object::IsPromise(self);
Steve Blocka7e24c12009-10-30 11:49:00 +00002996}
2997
2998
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002999MaybeLocal<String> Value::ToString(Local<Context> context) const {
3000 auto obj = Utils::OpenHandle(this);
3001 if (obj->IsString()) return ToApiHandle<String>(obj);
Ben Murdochc5610432016-08-08 18:44:38 +01003002 PREPARE_FOR_EXECUTION(context, Object, ToString, String);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003003 Local<String> result;
3004 has_pending_exception =
3005 !ToLocal<String>(i::Object::ToString(isolate, obj), &result);
3006 RETURN_ON_FAILED_EXECUTION(String);
3007 RETURN_ESCAPED(result);
Steve Blocka7e24c12009-10-30 11:49:00 +00003008}
3009
3010
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003011Local<String> Value::ToString(Isolate* isolate) const {
3012 RETURN_TO_LOCAL_UNCHECKED(ToString(isolate->GetCurrentContext()), String);
3013}
3014
3015
3016MaybeLocal<String> Value::ToDetailString(Local<Context> context) const {
Steve Blocka7e24c12009-10-30 11:49:00 +00003017 i::Handle<i::Object> obj = Utils::OpenHandle(this);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003018 if (obj->IsString()) return ToApiHandle<String>(obj);
Ben Murdochc5610432016-08-08 18:44:38 +01003019 PREPARE_FOR_EXECUTION(context, Object, ToDetailString, String);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003020 Local<String> result;
3021 i::Handle<i::Object> args[] = {obj};
3022 has_pending_exception = !ToLocal<String>(
3023 i::Execution::TryCall(isolate, isolate->no_side_effects_to_string_fun(),
3024 isolate->factory()->undefined_value(),
3025 arraysize(args), args),
3026 &result);
3027 RETURN_ON_FAILED_EXECUTION(String);
3028 RETURN_ESCAPED(result);
3029}
3030
3031
3032Local<String> Value::ToDetailString(Isolate* isolate) const {
3033 RETURN_TO_LOCAL_UNCHECKED(ToDetailString(isolate->GetCurrentContext()),
3034 String);
3035}
3036
3037
3038MaybeLocal<Object> Value::ToObject(Local<Context> context) const {
3039 auto obj = Utils::OpenHandle(this);
Ben Murdoch097c5b22016-05-18 11:27:45 +01003040 if (obj->IsJSReceiver()) return ToApiHandle<Object>(obj);
Ben Murdochc5610432016-08-08 18:44:38 +01003041 PREPARE_FOR_EXECUTION(context, Object, ToObject, Object);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003042 Local<Object> result;
3043 has_pending_exception =
Ben Murdoch097c5b22016-05-18 11:27:45 +01003044 !ToLocal<Object>(i::Object::ToObject(isolate, obj), &result);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003045 RETURN_ON_FAILED_EXECUTION(Object);
3046 RETURN_ESCAPED(result);
3047}
3048
3049
3050Local<v8::Object> Value::ToObject(Isolate* isolate) const {
3051 RETURN_TO_LOCAL_UNCHECKED(ToObject(isolate->GetCurrentContext()), Object);
3052}
3053
3054
3055MaybeLocal<Boolean> Value::ToBoolean(Local<Context> context) const {
3056 auto obj = Utils::OpenHandle(this);
3057 if (obj->IsBoolean()) return ToApiHandle<Boolean>(obj);
3058 auto isolate = reinterpret_cast<i::Isolate*>(context->GetIsolate());
3059 auto val = isolate->factory()->ToBoolean(obj->BooleanValue());
3060 return ToApiHandle<Boolean>(val);
Steve Blocka7e24c12009-10-30 11:49:00 +00003061}
3062
3063
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003064Local<Boolean> Value::ToBoolean(Isolate* v8_isolate) const {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003065 return ToBoolean(v8_isolate->GetCurrentContext()).ToLocalChecked();
Steve Blocka7e24c12009-10-30 11:49:00 +00003066}
3067
3068
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003069MaybeLocal<Number> Value::ToNumber(Local<Context> context) const {
3070 auto obj = Utils::OpenHandle(this);
3071 if (obj->IsNumber()) return ToApiHandle<Number>(obj);
Ben Murdochc5610432016-08-08 18:44:38 +01003072 PREPARE_FOR_EXECUTION(context, Object, ToNumber, Number);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003073 Local<Number> result;
3074 has_pending_exception = !ToLocal<Number>(i::Object::ToNumber(obj), &result);
3075 RETURN_ON_FAILED_EXECUTION(Number);
3076 RETURN_ESCAPED(result);
Steve Blocka7e24c12009-10-30 11:49:00 +00003077}
3078
3079
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003080Local<Number> Value::ToNumber(Isolate* isolate) const {
3081 RETURN_TO_LOCAL_UNCHECKED(ToNumber(isolate->GetCurrentContext()), Number);
3082}
3083
3084
3085MaybeLocal<Integer> Value::ToInteger(Local<Context> context) const {
3086 auto obj = Utils::OpenHandle(this);
3087 if (obj->IsSmi()) return ToApiHandle<Integer>(obj);
Ben Murdochc5610432016-08-08 18:44:38 +01003088 PREPARE_FOR_EXECUTION(context, Object, ToInteger, Integer);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003089 Local<Integer> result;
3090 has_pending_exception =
3091 !ToLocal<Integer>(i::Object::ToInteger(isolate, obj), &result);
3092 RETURN_ON_FAILED_EXECUTION(Integer);
3093 RETURN_ESCAPED(result);
3094}
3095
3096
3097Local<Integer> Value::ToInteger(Isolate* isolate) const {
3098 RETURN_TO_LOCAL_UNCHECKED(ToInteger(isolate->GetCurrentContext()), Integer);
3099}
3100
3101
3102MaybeLocal<Int32> Value::ToInt32(Local<Context> context) const {
3103 auto obj = Utils::OpenHandle(this);
3104 if (obj->IsSmi()) return ToApiHandle<Int32>(obj);
3105 Local<Int32> result;
Ben Murdochc5610432016-08-08 18:44:38 +01003106 PREPARE_FOR_EXECUTION(context, Object, ToInt32, Int32);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003107 has_pending_exception =
3108 !ToLocal<Int32>(i::Object::ToInt32(isolate, obj), &result);
3109 RETURN_ON_FAILED_EXECUTION(Int32);
3110 RETURN_ESCAPED(result);
3111}
3112
3113
3114Local<Int32> Value::ToInt32(Isolate* isolate) const {
3115 RETURN_TO_LOCAL_UNCHECKED(ToInt32(isolate->GetCurrentContext()), Int32);
3116}
3117
3118
3119MaybeLocal<Uint32> Value::ToUint32(Local<Context> context) const {
3120 auto obj = Utils::OpenHandle(this);
3121 if (obj->IsSmi()) return ToApiHandle<Uint32>(obj);
3122 Local<Uint32> result;
Ben Murdochc5610432016-08-08 18:44:38 +01003123 PREPARE_FOR_EXECUTION(context, Object, ToUint32, Uint32);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003124 has_pending_exception =
3125 !ToLocal<Uint32>(i::Object::ToUint32(isolate, obj), &result);
3126 RETURN_ON_FAILED_EXECUTION(Uint32);
3127 RETURN_ESCAPED(result);
3128}
3129
3130
3131Local<Uint32> Value::ToUint32(Isolate* isolate) const {
3132 RETURN_TO_LOCAL_UNCHECKED(ToUint32(isolate->GetCurrentContext()), Uint32);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003133}
3134
3135
3136void i::Internals::CheckInitializedImpl(v8::Isolate* external_isolate) {
3137 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(external_isolate);
Ben Murdochc5610432016-08-08 18:44:38 +01003138 Utils::ApiCheck(isolate != NULL && !isolate->IsDead(),
3139 "v8::internal::Internals::CheckInitialized",
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003140 "Isolate is not initialized or V8 has died");
Steve Blocka7e24c12009-10-30 11:49:00 +00003141}
3142
3143
3144void External::CheckCast(v8::Value* that) {
Ben Murdochc5610432016-08-08 18:44:38 +01003145 Utils::ApiCheck(Utils::OpenHandle(that)->IsExternal(), "v8::External::Cast",
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003146 "Could not convert to external");
Steve Blocka7e24c12009-10-30 11:49:00 +00003147}
3148
3149
3150void v8::Object::CheckCast(Value* that) {
Steve Blocka7e24c12009-10-30 11:49:00 +00003151 i::Handle<i::Object> obj = Utils::OpenHandle(that);
Ben Murdochc5610432016-08-08 18:44:38 +01003152 Utils::ApiCheck(obj->IsJSReceiver(), "v8::Object::Cast",
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003153 "Could not convert to object");
Steve Blocka7e24c12009-10-30 11:49:00 +00003154}
3155
3156
3157void v8::Function::CheckCast(Value* that) {
Steve Blocka7e24c12009-10-30 11:49:00 +00003158 i::Handle<i::Object> obj = Utils::OpenHandle(that);
Ben Murdochc5610432016-08-08 18:44:38 +01003159 Utils::ApiCheck(obj->IsCallable(), "v8::Function::Cast",
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003160 "Could not convert to function");
3161}
3162
3163
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003164void v8::Boolean::CheckCast(v8::Value* that) {
3165 i::Handle<i::Object> obj = Utils::OpenHandle(that);
Ben Murdochc5610432016-08-08 18:44:38 +01003166 Utils::ApiCheck(obj->IsBoolean(), "v8::Boolean::Cast",
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003167 "Could not convert to boolean");
3168}
3169
3170
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003171void v8::Name::CheckCast(v8::Value* that) {
3172 i::Handle<i::Object> obj = Utils::OpenHandle(that);
Ben Murdochc5610432016-08-08 18:44:38 +01003173 Utils::ApiCheck(obj->IsName(), "v8::Name::Cast", "Could not convert to name");
Steve Blocka7e24c12009-10-30 11:49:00 +00003174}
3175
3176
3177void v8::String::CheckCast(v8::Value* that) {
Steve Blocka7e24c12009-10-30 11:49:00 +00003178 i::Handle<i::Object> obj = Utils::OpenHandle(that);
Ben Murdochc5610432016-08-08 18:44:38 +01003179 Utils::ApiCheck(obj->IsString(), "v8::String::Cast",
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003180 "Could not convert to string");
3181}
3182
3183
3184void v8::Symbol::CheckCast(v8::Value* that) {
3185 i::Handle<i::Object> obj = Utils::OpenHandle(that);
Ben Murdochc5610432016-08-08 18:44:38 +01003186 Utils::ApiCheck(obj->IsSymbol(), "v8::Symbol::Cast",
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003187 "Could not convert to symbol");
Steve Blocka7e24c12009-10-30 11:49:00 +00003188}
3189
3190
3191void v8::Number::CheckCast(v8::Value* that) {
Steve Blocka7e24c12009-10-30 11:49:00 +00003192 i::Handle<i::Object> obj = Utils::OpenHandle(that);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003193 Utils::ApiCheck(obj->IsNumber(),
3194 "v8::Number::Cast()",
3195 "Could not convert to number");
Steve Blocka7e24c12009-10-30 11:49:00 +00003196}
3197
3198
3199void v8::Integer::CheckCast(v8::Value* that) {
Steve Blocka7e24c12009-10-30 11:49:00 +00003200 i::Handle<i::Object> obj = Utils::OpenHandle(that);
Ben Murdochc5610432016-08-08 18:44:38 +01003201 Utils::ApiCheck(obj->IsNumber(), "v8::Integer::Cast",
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003202 "Could not convert to number");
Steve Blocka7e24c12009-10-30 11:49:00 +00003203}
3204
3205
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003206void v8::Int32::CheckCast(v8::Value* that) {
Ben Murdochc5610432016-08-08 18:44:38 +01003207 Utils::ApiCheck(that->IsInt32(), "v8::Int32::Cast",
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003208 "Could not convert to 32-bit signed integer");
3209}
3210
3211
3212void v8::Uint32::CheckCast(v8::Value* that) {
Ben Murdochc5610432016-08-08 18:44:38 +01003213 Utils::ApiCheck(that->IsUint32(), "v8::Uint32::Cast",
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003214 "Could not convert to 32-bit unsigned integer");
3215}
3216
3217
Steve Blocka7e24c12009-10-30 11:49:00 +00003218void v8::Array::CheckCast(Value* that) {
Steve Blocka7e24c12009-10-30 11:49:00 +00003219 i::Handle<i::Object> obj = Utils::OpenHandle(that);
Ben Murdochc5610432016-08-08 18:44:38 +01003220 Utils::ApiCheck(obj->IsJSArray(), "v8::Array::Cast",
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003221 "Could not convert to array");
3222}
3223
3224
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003225void v8::Map::CheckCast(Value* that) {
3226 i::Handle<i::Object> obj = Utils::OpenHandle(that);
Ben Murdochc5610432016-08-08 18:44:38 +01003227 Utils::ApiCheck(obj->IsJSMap(), "v8::Map::Cast", "Could not convert to Map");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003228}
3229
3230
3231void v8::Set::CheckCast(Value* that) {
3232 i::Handle<i::Object> obj = Utils::OpenHandle(that);
Ben Murdochc5610432016-08-08 18:44:38 +01003233 Utils::ApiCheck(obj->IsJSSet(), "v8_Set_Cast", "Could not convert to Set");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003234}
3235
3236
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003237void v8::Promise::CheckCast(Value* that) {
Ben Murdochc5610432016-08-08 18:44:38 +01003238 Utils::ApiCheck(that->IsPromise(), "v8::Promise::Cast",
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003239 "Could not convert to promise");
3240}
3241
3242
3243void v8::Promise::Resolver::CheckCast(Value* that) {
Ben Murdochc5610432016-08-08 18:44:38 +01003244 Utils::ApiCheck(that->IsPromise(), "v8::Promise::Resolver::Cast",
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003245 "Could not convert to promise resolver");
3246}
3247
3248
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003249void v8::Proxy::CheckCast(Value* that) {
Ben Murdochc5610432016-08-08 18:44:38 +01003250 Utils::ApiCheck(that->IsProxy(), "v8::Proxy::Cast",
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003251 "Could not convert to proxy");
3252}
3253
3254
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003255void v8::ArrayBuffer::CheckCast(Value* that) {
3256 i::Handle<i::Object> obj = Utils::OpenHandle(that);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003257 Utils::ApiCheck(
3258 obj->IsJSArrayBuffer() && !i::JSArrayBuffer::cast(*obj)->is_shared(),
3259 "v8::ArrayBuffer::Cast()", "Could not convert to ArrayBuffer");
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003260}
3261
3262
3263void v8::ArrayBufferView::CheckCast(Value* that) {
3264 i::Handle<i::Object> obj = Utils::OpenHandle(that);
3265 Utils::ApiCheck(obj->IsJSArrayBufferView(),
3266 "v8::ArrayBufferView::Cast()",
3267 "Could not convert to ArrayBufferView");
3268}
3269
3270
3271void v8::TypedArray::CheckCast(Value* that) {
3272 i::Handle<i::Object> obj = Utils::OpenHandle(that);
3273 Utils::ApiCheck(obj->IsJSTypedArray(),
3274 "v8::TypedArray::Cast()",
3275 "Could not convert to TypedArray");
3276}
3277
3278
3279#define CHECK_TYPED_ARRAY_CAST(Type, typeName, TYPE, ctype, size) \
3280 void v8::Type##Array::CheckCast(Value* that) { \
3281 i::Handle<i::Object> obj = Utils::OpenHandle(that); \
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003282 Utils::ApiCheck( \
3283 obj->IsJSTypedArray() && \
3284 i::JSTypedArray::cast(*obj)->type() == i::kExternal##Type##Array, \
3285 "v8::" #Type "Array::Cast()", "Could not convert to " #Type "Array"); \
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003286 }
3287
3288
3289TYPED_ARRAYS(CHECK_TYPED_ARRAY_CAST)
3290
3291#undef CHECK_TYPED_ARRAY_CAST
3292
3293
3294void v8::DataView::CheckCast(Value* that) {
3295 i::Handle<i::Object> obj = Utils::OpenHandle(that);
3296 Utils::ApiCheck(obj->IsJSDataView(),
3297 "v8::DataView::Cast()",
3298 "Could not convert to DataView");
Steve Blocka7e24c12009-10-30 11:49:00 +00003299}
3300
3301
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003302void v8::SharedArrayBuffer::CheckCast(Value* that) {
3303 i::Handle<i::Object> obj = Utils::OpenHandle(that);
3304 Utils::ApiCheck(
3305 obj->IsJSArrayBuffer() && i::JSArrayBuffer::cast(*obj)->is_shared(),
3306 "v8::SharedArrayBuffer::Cast()",
3307 "Could not convert to SharedArrayBuffer");
3308}
3309
3310
Steve Blocka7e24c12009-10-30 11:49:00 +00003311void v8::Date::CheckCast(v8::Value* that) {
Steve Blocka7e24c12009-10-30 11:49:00 +00003312 i::Handle<i::Object> obj = Utils::OpenHandle(that);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003313 i::Isolate* isolate = NULL;
3314 if (obj->IsHeapObject()) isolate = i::HeapObject::cast(*obj)->GetIsolate();
3315 Utils::ApiCheck(isolate != NULL &&
3316 obj->HasSpecificClassOf(isolate->heap()->Date_string()),
3317 "v8::Date::Cast()",
3318 "Could not convert to date");
Steve Blocka7e24c12009-10-30 11:49:00 +00003319}
3320
3321
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003322void v8::StringObject::CheckCast(v8::Value* that) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003323 i::Handle<i::Object> obj = Utils::OpenHandle(that);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003324 i::Isolate* isolate = NULL;
3325 if (obj->IsHeapObject()) isolate = i::HeapObject::cast(*obj)->GetIsolate();
3326 Utils::ApiCheck(isolate != NULL &&
3327 obj->HasSpecificClassOf(isolate->heap()->String_string()),
3328 "v8::StringObject::Cast()",
3329 "Could not convert to StringObject");
3330}
3331
3332
3333void v8::SymbolObject::CheckCast(v8::Value* that) {
3334 i::Handle<i::Object> obj = Utils::OpenHandle(that);
3335 i::Isolate* isolate = NULL;
3336 if (obj->IsHeapObject()) isolate = i::HeapObject::cast(*obj)->GetIsolate();
3337 Utils::ApiCheck(isolate != NULL &&
3338 obj->HasSpecificClassOf(isolate->heap()->Symbol_string()),
3339 "v8::SymbolObject::Cast()",
3340 "Could not convert to SymbolObject");
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003341}
3342
3343
3344void v8::NumberObject::CheckCast(v8::Value* that) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003345 i::Handle<i::Object> obj = Utils::OpenHandle(that);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003346 i::Isolate* isolate = NULL;
3347 if (obj->IsHeapObject()) isolate = i::HeapObject::cast(*obj)->GetIsolate();
3348 Utils::ApiCheck(isolate != NULL &&
3349 obj->HasSpecificClassOf(isolate->heap()->Number_string()),
3350 "v8::NumberObject::Cast()",
3351 "Could not convert to NumberObject");
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003352}
3353
3354
3355void v8::BooleanObject::CheckCast(v8::Value* that) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003356 i::Handle<i::Object> obj = Utils::OpenHandle(that);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003357 i::Isolate* isolate = NULL;
3358 if (obj->IsHeapObject()) isolate = i::HeapObject::cast(*obj)->GetIsolate();
3359 Utils::ApiCheck(isolate != NULL &&
3360 obj->HasSpecificClassOf(isolate->heap()->Boolean_string()),
3361 "v8::BooleanObject::Cast()",
3362 "Could not convert to BooleanObject");
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003363}
3364
3365
Ben Murdochf87a2032010-10-22 12:50:53 +01003366void v8::RegExp::CheckCast(v8::Value* that) {
Ben Murdochf87a2032010-10-22 12:50:53 +01003367 i::Handle<i::Object> obj = Utils::OpenHandle(that);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003368 Utils::ApiCheck(obj->IsJSRegExp(),
3369 "v8::RegExp::Cast()",
3370 "Could not convert to regular expression");
Ben Murdochf87a2032010-10-22 12:50:53 +01003371}
3372
3373
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003374Maybe<bool> Value::BooleanValue(Local<Context> context) const {
3375 return Just(Utils::OpenHandle(this)->BooleanValue());
3376}
3377
3378
Steve Blocka7e24c12009-10-30 11:49:00 +00003379bool Value::BooleanValue() const {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003380 return Utils::OpenHandle(this)->BooleanValue();
Steve Blocka7e24c12009-10-30 11:49:00 +00003381}
3382
3383
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003384Maybe<double> Value::NumberValue(Local<Context> context) const {
3385 auto obj = Utils::OpenHandle(this);
3386 if (obj->IsNumber()) return Just(obj->Number());
Ben Murdochc5610432016-08-08 18:44:38 +01003387 PREPARE_FOR_EXECUTION_PRIMITIVE(context, Object, NumberValue, double);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003388 i::Handle<i::Object> num;
3389 has_pending_exception = !i::Object::ToNumber(obj).ToHandle(&num);
3390 RETURN_ON_FAILED_EXECUTION_PRIMITIVE(double);
3391 return Just(num->Number());
3392}
3393
3394
Steve Blocka7e24c12009-10-30 11:49:00 +00003395double Value::NumberValue() const {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003396 auto obj = Utils::OpenHandle(this);
3397 if (obj->IsNumber()) return obj->Number();
3398 return NumberValue(ContextFromHeapObject(obj))
3399 .FromMaybe(std::numeric_limits<double>::quiet_NaN());
3400}
3401
3402
3403Maybe<int64_t> Value::IntegerValue(Local<Context> context) const {
3404 auto obj = Utils::OpenHandle(this);
Steve Blocka7e24c12009-10-30 11:49:00 +00003405 if (obj->IsNumber()) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01003406 return Just(NumberToInt64(*obj));
Steve Blocka7e24c12009-10-30 11:49:00 +00003407 }
Ben Murdochc5610432016-08-08 18:44:38 +01003408 PREPARE_FOR_EXECUTION_PRIMITIVE(context, Object, IntegerValue, int64_t);
Ben Murdoch097c5b22016-05-18 11:27:45 +01003409 i::Handle<i::Object> num;
3410 has_pending_exception = !i::Object::ToInteger(isolate, obj).ToHandle(&num);
3411 RETURN_ON_FAILED_EXECUTION_PRIMITIVE(int64_t);
3412 return Just(NumberToInt64(*num));
Steve Blocka7e24c12009-10-30 11:49:00 +00003413}
3414
3415
3416int64_t Value::IntegerValue() const {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003417 auto obj = Utils::OpenHandle(this);
Steve Blocka7e24c12009-10-30 11:49:00 +00003418 if (obj->IsNumber()) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003419 if (obj->IsSmi()) {
3420 return i::Smi::cast(*obj)->value();
3421 } else {
3422 return static_cast<int64_t>(obj->Number());
3423 }
Steve Blocka7e24c12009-10-30 11:49:00 +00003424 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003425 return IntegerValue(ContextFromHeapObject(obj)).FromMaybe(0);
Steve Blocka7e24c12009-10-30 11:49:00 +00003426}
3427
3428
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003429Maybe<int32_t> Value::Int32Value(Local<Context> context) const {
3430 auto obj = Utils::OpenHandle(this);
3431 if (obj->IsNumber()) return Just(NumberToInt32(*obj));
Ben Murdochc5610432016-08-08 18:44:38 +01003432 PREPARE_FOR_EXECUTION_PRIMITIVE(context, Object, Int32Value, int32_t);
Steve Blocka7e24c12009-10-30 11:49:00 +00003433 i::Handle<i::Object> num;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003434 has_pending_exception = !i::Object::ToInt32(isolate, obj).ToHandle(&num);
3435 RETURN_ON_FAILED_EXECUTION_PRIMITIVE(int32_t);
3436 return Just(num->IsSmi() ? i::Smi::cast(*num)->value()
3437 : static_cast<int32_t>(num->Number()));
Steve Blocka7e24c12009-10-30 11:49:00 +00003438}
3439
3440
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003441int32_t Value::Int32Value() const {
3442 auto obj = Utils::OpenHandle(this);
3443 if (obj->IsNumber()) return NumberToInt32(*obj);
3444 return Int32Value(ContextFromHeapObject(obj)).FromMaybe(0);
3445}
3446
3447
3448Maybe<uint32_t> Value::Uint32Value(Local<Context> context) const {
3449 auto obj = Utils::OpenHandle(this);
3450 if (obj->IsNumber()) return Just(NumberToUint32(*obj));
Ben Murdochc5610432016-08-08 18:44:38 +01003451 PREPARE_FOR_EXECUTION_PRIMITIVE(context, Object, Uint32Value, uint32_t);
Steve Blocka7e24c12009-10-30 11:49:00 +00003452 i::Handle<i::Object> num;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003453 has_pending_exception = !i::Object::ToUint32(isolate, obj).ToHandle(&num);
3454 RETURN_ON_FAILED_EXECUTION_PRIMITIVE(uint32_t);
3455 return Just(num->IsSmi() ? static_cast<uint32_t>(i::Smi::cast(*num)->value())
3456 : static_cast<uint32_t>(num->Number()));
Steve Blocka7e24c12009-10-30 11:49:00 +00003457}
3458
3459
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003460uint32_t Value::Uint32Value() const {
3461 auto obj = Utils::OpenHandle(this);
3462 if (obj->IsNumber()) return NumberToUint32(*obj);
3463 return Uint32Value(ContextFromHeapObject(obj)).FromMaybe(0);
3464}
3465
3466
3467MaybeLocal<Uint32> Value::ToArrayIndex(Local<Context> context) const {
3468 auto self = Utils::OpenHandle(this);
3469 if (self->IsSmi()) {
3470 if (i::Smi::cast(*self)->value() >= 0) return Utils::Uint32ToLocal(self);
Steve Blocka7e24c12009-10-30 11:49:00 +00003471 return Local<Uint32>();
3472 }
Ben Murdochc5610432016-08-08 18:44:38 +01003473 PREPARE_FOR_EXECUTION(context, Object, ToArrayIndex, Uint32);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003474 i::Handle<i::Object> string_obj;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003475 has_pending_exception =
3476 !i::Object::ToString(isolate, self).ToHandle(&string_obj);
3477 RETURN_ON_FAILED_EXECUTION(Uint32);
Steve Blocka7e24c12009-10-30 11:49:00 +00003478 i::Handle<i::String> str = i::Handle<i::String>::cast(string_obj);
3479 uint32_t index;
3480 if (str->AsArrayIndex(&index)) {
3481 i::Handle<i::Object> value;
3482 if (index <= static_cast<uint32_t>(i::Smi::kMaxValue)) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003483 value = i::Handle<i::Object>(i::Smi::FromInt(index), isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00003484 } else {
Steve Block44f0eee2011-05-26 01:26:41 +01003485 value = isolate->factory()->NewNumber(index);
Steve Blocka7e24c12009-10-30 11:49:00 +00003486 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003487 RETURN_ESCAPED(Utils::Uint32ToLocal(value));
Steve Blocka7e24c12009-10-30 11:49:00 +00003488 }
3489 return Local<Uint32>();
3490}
3491
3492
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003493Local<Uint32> Value::ToArrayIndex() const {
3494 auto self = Utils::OpenHandle(this);
3495 if (self->IsSmi()) {
3496 if (i::Smi::cast(*self)->value() >= 0) return Utils::Uint32ToLocal(self);
3497 return Local<Uint32>();
Steve Blocka7e24c12009-10-30 11:49:00 +00003498 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003499 auto context = ContextFromHeapObject(self);
3500 RETURN_TO_LOCAL_UNCHECKED(ToArrayIndex(context), Uint32);
Steve Blocka7e24c12009-10-30 11:49:00 +00003501}
3502
3503
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003504Maybe<bool> Value::Equals(Local<Context> context, Local<Value> that) const {
3505 auto self = Utils::OpenHandle(this);
3506 auto other = Utils::OpenHandle(*that);
3507 return i::Object::Equals(self, other);
Steve Blocka7e24c12009-10-30 11:49:00 +00003508}
3509
3510
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003511bool Value::Equals(Local<Value> that) const {
3512 auto self = Utils::OpenHandle(this);
3513 auto other = Utils::OpenHandle(*that);
3514 if (self->IsSmi() && other->IsSmi()) {
3515 return self->Number() == other->Number();
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003516 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003517 if (self->IsJSObject() && other->IsJSObject()) {
3518 return *self == *other;
Steve Blocka7e24c12009-10-30 11:49:00 +00003519 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003520 auto heap_object = self->IsSmi() ? other : self;
3521 auto context = ContextFromHeapObject(heap_object);
3522 return Equals(context, that).FromMaybe(false);
Steve Blocka7e24c12009-10-30 11:49:00 +00003523}
3524
3525
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003526bool Value::StrictEquals(Local<Value> that) const {
3527 auto self = Utils::OpenHandle(this);
3528 auto other = Utils::OpenHandle(*that);
3529 return self->StrictEquals(*other);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003530}
3531
3532
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003533bool Value::SameValue(Local<Value> that) const {
3534 auto self = Utils::OpenHandle(this);
3535 auto other = Utils::OpenHandle(*that);
3536 return self->SameValue(*other);
Steve Blocka7e24c12009-10-30 11:49:00 +00003537}
3538
Ben Murdochc5610432016-08-08 18:44:38 +01003539Local<String> Value::TypeOf(v8::Isolate* external_isolate) {
3540 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(external_isolate);
3541 ENTER_V8(isolate);
3542 LOG_API(isolate, Value, TypeOf);
3543 return Utils::ToLocal(i::Object::TypeOf(isolate, Utils::OpenHandle(this)));
3544}
Steve Blocka7e24c12009-10-30 11:49:00 +00003545
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003546Maybe<bool> v8::Object::Set(v8::Local<v8::Context> context,
3547 v8::Local<Value> key, v8::Local<Value> value) {
Ben Murdochc5610432016-08-08 18:44:38 +01003548 PREPARE_FOR_EXECUTION_PRIMITIVE(context, Object, Set, bool);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003549 auto self = Utils::OpenHandle(this);
3550 auto key_obj = Utils::OpenHandle(*key);
3551 auto value_obj = Utils::OpenHandle(*value);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003552 has_pending_exception =
3553 i::Runtime::SetObjectProperty(isolate, self, key_obj, value_obj,
3554 i::SLOPPY).is_null();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003555 RETURN_ON_FAILED_EXECUTION_PRIMITIVE(bool);
3556 return Just(true);
Steve Blocka7e24c12009-10-30 11:49:00 +00003557}
3558
3559
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003560bool v8::Object::Set(v8::Local<Value> key, v8::Local<Value> value) {
3561 auto context = ContextFromHeapObject(Utils::OpenHandle(this));
3562 return Set(context, key, value).FromMaybe(false);
3563}
3564
3565
3566Maybe<bool> v8::Object::Set(v8::Local<v8::Context> context, uint32_t index,
3567 v8::Local<Value> value) {
Ben Murdochc5610432016-08-08 18:44:38 +01003568 PREPARE_FOR_EXECUTION_PRIMITIVE(context, Object, Set, bool);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003569 auto self = Utils::OpenHandle(this);
3570 auto value_obj = Utils::OpenHandle(*value);
3571 has_pending_exception = i::Object::SetElement(isolate, self, index, value_obj,
3572 i::SLOPPY).is_null();
3573 RETURN_ON_FAILED_EXECUTION_PRIMITIVE(bool);
3574 return Just(true);
3575}
3576
3577
3578bool v8::Object::Set(uint32_t index, v8::Local<Value> value) {
3579 auto context = ContextFromHeapObject(Utils::OpenHandle(this));
3580 return Set(context, index, value).FromMaybe(false);
3581}
3582
3583
3584Maybe<bool> v8::Object::CreateDataProperty(v8::Local<v8::Context> context,
3585 v8::Local<Name> key,
3586 v8::Local<Value> value) {
Ben Murdochc5610432016-08-08 18:44:38 +01003587 PREPARE_FOR_EXECUTION_PRIMITIVE(context, Object, CreateDataProperty, bool);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003588 i::Handle<i::JSReceiver> self = Utils::OpenHandle(this);
3589 i::Handle<i::Name> key_obj = Utils::OpenHandle(*key);
Steve Block6ded16b2010-05-10 14:33:55 +01003590 i::Handle<i::Object> value_obj = Utils::OpenHandle(*value);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003591
3592 i::LookupIterator it = i::LookupIterator::PropertyOrElement(
Ben Murdochda12d292016-06-02 14:46:10 +01003593 isolate, self, key_obj, self, i::LookupIterator::OWN);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003594 Maybe<bool> result =
3595 i::JSReceiver::CreateDataProperty(&it, value_obj, i::Object::DONT_THROW);
3596 has_pending_exception = result.IsNothing();
3597 RETURN_ON_FAILED_EXECUTION_PRIMITIVE(bool);
3598 return result;
Steve Block6ded16b2010-05-10 14:33:55 +01003599}
3600
3601
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003602Maybe<bool> v8::Object::CreateDataProperty(v8::Local<v8::Context> context,
3603 uint32_t index,
3604 v8::Local<Value> value) {
Ben Murdochc5610432016-08-08 18:44:38 +01003605 PREPARE_FOR_EXECUTION_PRIMITIVE(context, Object, CreateDataProperty, bool);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003606 i::Handle<i::JSReceiver> self = Utils::OpenHandle(this);
3607 i::Handle<i::Object> value_obj = Utils::OpenHandle(*value);
3608
Ben Murdochda12d292016-06-02 14:46:10 +01003609 i::LookupIterator it(isolate, self, index, self, i::LookupIterator::OWN);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003610 Maybe<bool> result =
3611 i::JSReceiver::CreateDataProperty(&it, value_obj, i::Object::DONT_THROW);
3612 has_pending_exception = result.IsNothing();
3613 RETURN_ON_FAILED_EXECUTION_PRIMITIVE(bool);
3614 return result;
3615}
3616
3617
3618Maybe<bool> v8::Object::DefineOwnProperty(v8::Local<v8::Context> context,
3619 v8::Local<Name> key,
3620 v8::Local<Value> value,
3621 v8::PropertyAttribute attributes) {
Ben Murdochc5610432016-08-08 18:44:38 +01003622 PREPARE_FOR_EXECUTION_PRIMITIVE(context, Object, DefineOwnProperty, bool);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003623 i::Handle<i::JSReceiver> self = Utils::OpenHandle(this);
3624 i::Handle<i::Name> key_obj = Utils::OpenHandle(*key);
3625 i::Handle<i::Object> value_obj = Utils::OpenHandle(*value);
3626
3627 if (self->IsAccessCheckNeeded() &&
3628 !isolate->MayAccess(handle(isolate->context()),
3629 i::Handle<i::JSObject>::cast(self))) {
3630 isolate->ReportFailedAccessCheck(i::Handle<i::JSObject>::cast(self));
3631 return Nothing<bool>();
3632 }
3633
3634 i::PropertyDescriptor desc;
3635 desc.set_writable(!(attributes & v8::ReadOnly));
3636 desc.set_enumerable(!(attributes & v8::DontEnum));
3637 desc.set_configurable(!(attributes & v8::DontDelete));
3638 desc.set_value(value_obj);
3639 Maybe<bool> success = i::JSReceiver::DefineOwnProperty(
3640 isolate, self, key_obj, &desc, i::Object::DONT_THROW);
3641 // Even though we said DONT_THROW, there might be accessors that do throw.
3642 RETURN_ON_FAILED_EXECUTION_PRIMITIVE(bool);
3643 return success;
3644}
3645
3646
3647MUST_USE_RESULT
3648static i::MaybeHandle<i::Object> DefineObjectProperty(
3649 i::Handle<i::JSObject> js_object, i::Handle<i::Object> key,
3650 i::Handle<i::Object> value, i::PropertyAttributes attrs) {
3651 i::Isolate* isolate = js_object->GetIsolate();
3652 bool success = false;
3653 i::LookupIterator it = i::LookupIterator::PropertyOrElement(
3654 isolate, js_object, key, &success, i::LookupIterator::OWN);
3655 if (!success) return i::MaybeHandle<i::Object>();
3656
Ben Murdoch097c5b22016-05-18 11:27:45 +01003657 return i::JSObject::DefineOwnPropertyIgnoreAttributes(
3658 &it, value, attrs, i::JSObject::FORCE_FIELD);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003659}
3660
3661
3662Maybe<bool> v8::Object::ForceSet(v8::Local<v8::Context> context,
3663 v8::Local<Value> key, v8::Local<Value> value,
3664 v8::PropertyAttribute attribs) {
Ben Murdochc5610432016-08-08 18:44:38 +01003665 PREPARE_FOR_EXECUTION_PRIMITIVE(context, Object, ForceSet, bool);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003666 auto self = i::Handle<i::JSObject>::cast(Utils::OpenHandle(this));
3667 auto key_obj = Utils::OpenHandle(*key);
3668 auto value_obj = Utils::OpenHandle(*value);
3669 has_pending_exception =
3670 DefineObjectProperty(self, key_obj, value_obj,
3671 static_cast<i::PropertyAttributes>(attribs))
3672 .is_null();
3673 RETURN_ON_FAILED_EXECUTION_PRIMITIVE(bool);
3674 return Just(true);
3675}
3676
3677
3678bool v8::Object::ForceSet(v8::Local<Value> key, v8::Local<Value> value,
Steve Blocka7e24c12009-10-30 11:49:00 +00003679 v8::PropertyAttribute attribs) {
Steve Block44f0eee2011-05-26 01:26:41 +01003680 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
Ben Murdochc5610432016-08-08 18:44:38 +01003681 PREPARE_FOR_EXECUTION_GENERIC(isolate, Local<Context>(), Object, ForceSet,
3682 false, i::HandleScope, false);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003683 i::Handle<i::JSObject> self =
3684 i::Handle<i::JSObject>::cast(Utils::OpenHandle(this));
Steve Blocka7e24c12009-10-30 11:49:00 +00003685 i::Handle<i::Object> key_obj = Utils::OpenHandle(*key);
3686 i::Handle<i::Object> value_obj = Utils::OpenHandle(*value);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003687 has_pending_exception =
3688 DefineObjectProperty(self, key_obj, value_obj,
3689 static_cast<i::PropertyAttributes>(attribs))
3690 .is_null();
3691 EXCEPTION_BAILOUT_CHECK_SCOPED(isolate, false);
Steve Blocka7e24c12009-10-30 11:49:00 +00003692 return true;
3693}
3694
3695
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003696Maybe<bool> v8::Object::SetPrivate(Local<Context> context, Local<Private> key,
3697 Local<Value> value) {
Ben Murdochc5610432016-08-08 18:44:38 +01003698 PREPARE_FOR_EXECUTION_PRIMITIVE(context, Object, SetPrivate, bool);
Ben Murdoch097c5b22016-05-18 11:27:45 +01003699 auto self = Utils::OpenHandle(this);
3700 auto key_obj = Utils::OpenHandle(reinterpret_cast<Name*>(*key));
3701 auto value_obj = Utils::OpenHandle(*value);
3702 if (self->IsJSProxy()) {
3703 i::PropertyDescriptor desc;
3704 desc.set_writable(true);
3705 desc.set_enumerable(false);
3706 desc.set_configurable(true);
3707 desc.set_value(value_obj);
3708 return i::JSProxy::SetPrivateProperty(
3709 isolate, i::Handle<i::JSProxy>::cast(self),
3710 i::Handle<i::Symbol>::cast(key_obj), &desc, i::Object::DONT_THROW);
3711 }
3712 auto js_object = i::Handle<i::JSObject>::cast(self);
Ben Murdochda12d292016-06-02 14:46:10 +01003713 i::LookupIterator it(js_object, key_obj, js_object);
Ben Murdoch097c5b22016-05-18 11:27:45 +01003714 has_pending_exception = i::JSObject::DefineOwnPropertyIgnoreAttributes(
3715 &it, value_obj, i::DONT_ENUM)
3716 .is_null();
3717 RETURN_ON_FAILED_EXECUTION_PRIMITIVE(bool);
3718 return Just(true);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003719}
3720
3721
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003722MaybeLocal<Value> v8::Object::Get(Local<v8::Context> context,
3723 Local<Value> key) {
Ben Murdochc5610432016-08-08 18:44:38 +01003724 PREPARE_FOR_EXECUTION(context, Object, Get, Value);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003725 auto self = Utils::OpenHandle(this);
3726 auto key_obj = Utils::OpenHandle(*key);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003727 i::Handle<i::Object> result;
3728 has_pending_exception =
3729 !i::Runtime::GetObjectProperty(isolate, self, key_obj).ToHandle(&result);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003730 RETURN_ON_FAILED_EXECUTION(Value);
3731 RETURN_ESCAPED(Utils::ToLocal(result));
3732}
3733
3734
3735Local<Value> v8::Object::Get(v8::Local<Value> key) {
3736 auto context = ContextFromHeapObject(Utils::OpenHandle(this));
3737 RETURN_TO_LOCAL_UNCHECKED(Get(context, key), Value);
3738}
3739
3740
3741MaybeLocal<Value> v8::Object::Get(Local<Context> context, uint32_t index) {
Ben Murdochc5610432016-08-08 18:44:38 +01003742 PREPARE_FOR_EXECUTION(context, Object, Get, Value);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003743 auto self = Utils::OpenHandle(this);
3744 i::Handle<i::Object> result;
3745 has_pending_exception =
Ben Murdochda12d292016-06-02 14:46:10 +01003746 !i::JSReceiver::GetElement(isolate, self, index).ToHandle(&result);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003747 RETURN_ON_FAILED_EXECUTION(Value);
3748 RETURN_ESCAPED(Utils::ToLocal(result));
Steve Blocka7e24c12009-10-30 11:49:00 +00003749}
3750
3751
Steve Block6ded16b2010-05-10 14:33:55 +01003752Local<Value> v8::Object::Get(uint32_t index) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003753 auto context = ContextFromHeapObject(Utils::OpenHandle(this));
3754 RETURN_TO_LOCAL_UNCHECKED(Get(context, index), Value);
Steve Block6ded16b2010-05-10 14:33:55 +01003755}
3756
3757
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003758MaybeLocal<Value> v8::Object::GetPrivate(Local<Context> context,
3759 Local<Private> key) {
3760 return Get(context, Local<Value>(reinterpret_cast<Value*>(*key)));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003761}
3762
3763
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003764Maybe<PropertyAttribute> v8::Object::GetPropertyAttributes(
3765 Local<Context> context, Local<Value> key) {
Ben Murdochc5610432016-08-08 18:44:38 +01003766 PREPARE_FOR_EXECUTION_PRIMITIVE(context, Object, GetPropertyAttributes,
3767 PropertyAttribute);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003768 auto self = Utils::OpenHandle(this);
3769 auto key_obj = Utils::OpenHandle(*key);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003770 if (!key_obj->IsName()) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003771 has_pending_exception =
3772 !i::Object::ToString(isolate, key_obj).ToHandle(&key_obj);
3773 RETURN_ON_FAILED_EXECUTION_PRIMITIVE(PropertyAttribute);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003774 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003775 auto key_name = i::Handle<i::Name>::cast(key_obj);
3776 auto result = i::JSReceiver::GetPropertyAttributes(self, key_name);
3777 has_pending_exception = result.IsNothing();
3778 RETURN_ON_FAILED_EXECUTION_PRIMITIVE(PropertyAttribute);
3779 if (result.FromJust() == i::ABSENT) {
3780 return Just(static_cast<PropertyAttribute>(i::NONE));
3781 }
3782 return Just(static_cast<PropertyAttribute>(result.FromJust()));
3783}
3784
3785
3786PropertyAttribute v8::Object::GetPropertyAttributes(v8::Local<Value> key) {
3787 auto context = ContextFromHeapObject(Utils::OpenHandle(this));
3788 return GetPropertyAttributes(context, key)
3789 .FromMaybe(static_cast<PropertyAttribute>(i::NONE));
3790}
3791
3792
3793MaybeLocal<Value> v8::Object::GetOwnPropertyDescriptor(Local<Context> context,
3794 Local<String> key) {
Ben Murdochc5610432016-08-08 18:44:38 +01003795 PREPARE_FOR_EXECUTION(context, Object, GetOwnPropertyDescriptor, Value);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003796 i::Handle<i::JSReceiver> obj = Utils::OpenHandle(this);
3797 i::Handle<i::String> key_name = Utils::OpenHandle(*key);
3798
3799 i::PropertyDescriptor desc;
3800 Maybe<bool> found =
3801 i::JSReceiver::GetOwnPropertyDescriptor(isolate, obj, key_name, &desc);
3802 has_pending_exception = found.IsNothing();
3803 RETURN_ON_FAILED_EXECUTION(Value);
3804 if (!found.FromJust()) {
3805 return v8::Undefined(reinterpret_cast<v8::Isolate*>(isolate));
3806 }
3807 RETURN_ESCAPED(Utils::ToLocal(desc.ToObject(isolate)));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003808}
3809
3810
3811Local<Value> v8::Object::GetOwnPropertyDescriptor(Local<String> key) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003812 auto context = ContextFromHeapObject(Utils::OpenHandle(this));
3813 RETURN_TO_LOCAL_UNCHECKED(GetOwnPropertyDescriptor(context, key), Value);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003814}
3815
3816
Steve Blocka7e24c12009-10-30 11:49:00 +00003817Local<Value> v8::Object::GetPrototype() {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003818 auto isolate = Utils::OpenHandle(this)->GetIsolate();
3819 auto self = Utils::OpenHandle(this);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003820 i::PrototypeIterator iter(isolate, self);
3821 return Utils::ToLocal(i::PrototypeIterator::GetCurrent(iter));
Steve Blocka7e24c12009-10-30 11:49:00 +00003822}
3823
3824
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003825Maybe<bool> v8::Object::SetPrototype(Local<Context> context,
3826 Local<Value> value) {
Ben Murdochc5610432016-08-08 18:44:38 +01003827 PREPARE_FOR_EXECUTION_PRIMITIVE(context, Object, SetPrototype, bool);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003828 auto self = Utils::OpenHandle(this);
3829 auto value_obj = Utils::OpenHandle(*value);
Ben Murdoch8b112d22011-06-08 16:22:53 +01003830 // We do not allow exceptions thrown while setting the prototype
3831 // to propagate outside.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003832 TryCatch try_catch(reinterpret_cast<v8::Isolate*>(isolate));
3833 auto result = i::JSReceiver::SetPrototype(self, value_obj, false,
3834 i::Object::THROW_ON_ERROR);
3835 has_pending_exception = result.IsNothing();
3836 RETURN_ON_FAILED_EXECUTION_PRIMITIVE(bool);
3837 return Just(true);
3838}
3839
3840
3841bool v8::Object::SetPrototype(Local<Value> value) {
3842 auto context = ContextFromHeapObject(Utils::OpenHandle(this));
3843 return SetPrototype(context, value).FromMaybe(false);
Andrei Popescu402d9372010-02-26 13:31:12 +00003844}
3845
3846
Steve Blocka7e24c12009-10-30 11:49:00 +00003847Local<Object> v8::Object::FindInstanceInPrototypeChain(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003848 v8::Local<FunctionTemplate> tmpl) {
3849 auto isolate = Utils::OpenHandle(this)->GetIsolate();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003850 i::PrototypeIterator iter(isolate, *Utils::OpenHandle(this),
3851 i::PrototypeIterator::START_AT_RECEIVER);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003852 auto tmpl_info = *Utils::OpenHandle(*tmpl);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003853 while (!tmpl_info->IsTemplateFor(iter.GetCurrent())) {
3854 iter.Advance();
3855 if (iter.IsAtEnd()) {
3856 return Local<Object>();
3857 }
Steve Blocka7e24c12009-10-30 11:49:00 +00003858 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003859 // IsTemplateFor() ensures that iter.GetCurrent() can't be a Proxy here.
3860 return Utils::ToLocal(i::handle(iter.GetCurrent<i::JSObject>(), isolate));
3861}
3862
3863
3864MaybeLocal<Array> v8::Object::GetPropertyNames(Local<Context> context) {
Ben Murdochc5610432016-08-08 18:44:38 +01003865 PREPARE_FOR_EXECUTION(context, Object, GetPropertyNames, Array);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003866 auto self = Utils::OpenHandle(this);
3867 i::Handle<i::FixedArray> value;
3868 has_pending_exception =
Ben Murdoch097c5b22016-05-18 11:27:45 +01003869 !i::JSReceiver::GetKeys(self, i::INCLUDE_PROTOS, i::ENUMERABLE_STRINGS)
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003870 .ToHandle(&value);
3871 RETURN_ON_FAILED_EXECUTION(Array);
Ben Murdochda12d292016-06-02 14:46:10 +01003872 DCHECK(self->map()->EnumLength() == i::kInvalidEnumCacheSentinel ||
3873 self->map()->EnumLength() == 0 ||
3874 self->map()->instance_descriptors()->GetEnumCache() != *value);
3875 auto result = isolate->factory()->NewJSArrayWithElements(value);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003876 RETURN_ESCAPED(Utils::ToLocal(result));
Steve Blocka7e24c12009-10-30 11:49:00 +00003877}
3878
3879
3880Local<Array> v8::Object::GetPropertyNames() {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003881 auto context = ContextFromHeapObject(Utils::OpenHandle(this));
3882 RETURN_TO_LOCAL_UNCHECKED(GetPropertyNames(context), Array);
3883}
3884
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003885MaybeLocal<Array> v8::Object::GetOwnPropertyNames(Local<Context> context) {
Ben Murdochc5610432016-08-08 18:44:38 +01003886 return GetOwnPropertyNames(
3887 context, static_cast<v8::PropertyFilter>(ONLY_ENUMERABLE | SKIP_SYMBOLS));
3888}
3889
3890Local<Array> v8::Object::GetOwnPropertyNames() {
3891 auto context = ContextFromHeapObject(Utils::OpenHandle(this));
3892 RETURN_TO_LOCAL_UNCHECKED(GetOwnPropertyNames(context), Array);
3893}
3894
3895MaybeLocal<Array> v8::Object::GetOwnPropertyNames(Local<Context> context,
3896 PropertyFilter filter) {
3897 PREPARE_FOR_EXECUTION(context, Object, GetOwnPropertyNames, Array);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003898 auto self = Utils::OpenHandle(this);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003899 i::Handle<i::FixedArray> value;
Ben Murdoch097c5b22016-05-18 11:27:45 +01003900 has_pending_exception =
Ben Murdochc5610432016-08-08 18:44:38 +01003901 !i::JSReceiver::GetKeys(self, i::OWN_ONLY,
3902 static_cast<i::PropertyFilter>(filter))
Ben Murdoch097c5b22016-05-18 11:27:45 +01003903 .ToHandle(&value);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003904 RETURN_ON_FAILED_EXECUTION(Array);
Ben Murdochda12d292016-06-02 14:46:10 +01003905 DCHECK(self->map()->EnumLength() == i::kInvalidEnumCacheSentinel ||
3906 self->map()->EnumLength() == 0 ||
3907 self->map()->instance_descriptors()->GetEnumCache() != *value);
3908 auto result = isolate->factory()->NewJSArrayWithElements(value);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003909 RETURN_ESCAPED(Utils::ToLocal(result));
Steve Blocka7e24c12009-10-30 11:49:00 +00003910}
3911
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003912MaybeLocal<String> v8::Object::ObjectProtoToString(Local<Context> context) {
Ben Murdochc5610432016-08-08 18:44:38 +01003913 PREPARE_FOR_EXECUTION(context, Object, ObjectProtoToString, String);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003914 auto obj = Utils::OpenHandle(this);
3915 Local<String> result;
3916 has_pending_exception =
3917 !ToLocal<String>(i::JSObject::ObjectProtoToString(isolate, obj), &result);
3918 RETURN_ON_FAILED_EXECUTION(String);
3919 RETURN_ESCAPED(result);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003920}
3921
3922
Steve Blocka7e24c12009-10-30 11:49:00 +00003923Local<String> v8::Object::ObjectProtoToString() {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003924 auto context = ContextFromHeapObject(Utils::OpenHandle(this));
3925 RETURN_TO_LOCAL_UNCHECKED(ObjectProtoToString(context), String);
Steve Blocka7e24c12009-10-30 11:49:00 +00003926}
3927
3928
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08003929Local<String> v8::Object::GetConstructorName() {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003930 auto self = Utils::OpenHandle(this);
3931 i::Handle<i::String> name = i::JSReceiver::GetConstructorName(self);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08003932 return Utils::ToLocal(name);
3933}
3934
Ben Murdochda12d292016-06-02 14:46:10 +01003935Maybe<bool> v8::Object::SetIntegrityLevel(Local<Context> context,
3936 IntegrityLevel level) {
Ben Murdochc5610432016-08-08 18:44:38 +01003937 PREPARE_FOR_EXECUTION_PRIMITIVE(context, Object, SetIntegrityLevel, bool);
Ben Murdochda12d292016-06-02 14:46:10 +01003938 auto self = Utils::OpenHandle(this);
3939 i::JSReceiver::IntegrityLevel i_level =
3940 level == IntegrityLevel::kFrozen ? i::FROZEN : i::SEALED;
3941 Maybe<bool> result =
3942 i::JSReceiver::SetIntegrityLevel(self, i_level, i::Object::DONT_THROW);
3943 has_pending_exception = result.IsNothing();
3944 RETURN_ON_FAILED_EXECUTION_PRIMITIVE(bool);
3945 return result;
3946}
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08003947
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003948Maybe<bool> v8::Object::Delete(Local<Context> context, Local<Value> key) {
Ben Murdochc5610432016-08-08 18:44:38 +01003949 PREPARE_FOR_EXECUTION_PRIMITIVE(context, Object, Delete, bool);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003950 auto self = Utils::OpenHandle(this);
3951 auto key_obj = Utils::OpenHandle(*key);
3952 Maybe<bool> result =
3953 i::Runtime::DeleteObjectProperty(isolate, self, key_obj, i::SLOPPY);
3954 has_pending_exception = result.IsNothing();
3955 RETURN_ON_FAILED_EXECUTION_PRIMITIVE(bool);
3956 return result;
Steve Blocka7e24c12009-10-30 11:49:00 +00003957}
3958
3959
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003960bool v8::Object::Delete(v8::Local<Value> key) {
3961 auto context = ContextFromHeapObject(Utils::OpenHandle(this));
3962 return Delete(context, key).FromMaybe(false);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003963}
3964
3965
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003966Maybe<bool> v8::Object::DeletePrivate(Local<Context> context,
3967 Local<Private> key) {
3968 return Delete(context, Local<Value>(reinterpret_cast<Value*>(*key)));
3969}
3970
3971
3972Maybe<bool> v8::Object::Has(Local<Context> context, Local<Value> key) {
Ben Murdochc5610432016-08-08 18:44:38 +01003973 PREPARE_FOR_EXECUTION_PRIMITIVE(context, Object, Get, bool);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003974 auto self = Utils::OpenHandle(this);
3975 auto key_obj = Utils::OpenHandle(*key);
3976 Maybe<bool> maybe = Nothing<bool>();
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003977 // Check if the given key is an array index.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003978 uint32_t index = 0;
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003979 if (key_obj->ToArrayIndex(&index)) {
3980 maybe = i::JSReceiver::HasElement(self, index);
3981 } else {
3982 // Convert the key to a name - possibly by calling back into JavaScript.
3983 i::Handle<i::Name> name;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003984 if (i::Object::ToName(isolate, key_obj).ToHandle(&name)) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003985 maybe = i::JSReceiver::HasProperty(self, name);
3986 }
3987 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003988 has_pending_exception = maybe.IsNothing();
3989 RETURN_ON_FAILED_EXECUTION_PRIMITIVE(bool);
3990 return maybe;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003991}
3992
3993
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003994bool v8::Object::Has(v8::Local<Value> key) {
3995 auto context = ContextFromHeapObject(Utils::OpenHandle(this));
3996 return Has(context, key).FromMaybe(false);
3997}
3998
3999
4000Maybe<bool> v8::Object::HasPrivate(Local<Context> context, Local<Private> key) {
4001 return HasOwnProperty(context, Local<Name>(reinterpret_cast<Name*>(*key)));
4002}
4003
4004
4005Maybe<bool> v8::Object::Delete(Local<Context> context, uint32_t index) {
Ben Murdochc5610432016-08-08 18:44:38 +01004006 PREPARE_FOR_EXECUTION_PRIMITIVE(context, Object, DeleteProperty, bool);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004007 auto self = Utils::OpenHandle(this);
4008 Maybe<bool> result = i::JSReceiver::DeleteElement(self, index);
4009 has_pending_exception = result.IsNothing();
4010 RETURN_ON_FAILED_EXECUTION_PRIMITIVE(bool);
4011 return result;
Steve Blocka7e24c12009-10-30 11:49:00 +00004012}
4013
4014
4015bool v8::Object::Delete(uint32_t index) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004016 auto context = ContextFromHeapObject(Utils::OpenHandle(this));
4017 return Delete(context, index).FromMaybe(false);
4018}
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004019
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004020
4021Maybe<bool> v8::Object::Has(Local<Context> context, uint32_t index) {
Ben Murdochc5610432016-08-08 18:44:38 +01004022 PREPARE_FOR_EXECUTION_PRIMITIVE(context, Object, Get, bool);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004023 auto self = Utils::OpenHandle(this);
4024 auto maybe = i::JSReceiver::HasElement(self, index);
4025 has_pending_exception = maybe.IsNothing();
4026 RETURN_ON_FAILED_EXECUTION_PRIMITIVE(bool);
4027 return maybe;
Steve Blocka7e24c12009-10-30 11:49:00 +00004028}
4029
4030
4031bool v8::Object::Has(uint32_t index) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004032 auto context = ContextFromHeapObject(Utils::OpenHandle(this));
4033 return Has(context, index).FromMaybe(false);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004034}
4035
4036
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004037template <typename Getter, typename Setter, typename Data>
4038static Maybe<bool> ObjectSetAccessor(Local<Context> context, Object* self,
4039 Local<Name> name, Getter getter,
4040 Setter setter, Data data,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004041 AccessControl settings,
4042 PropertyAttribute attributes) {
Ben Murdochc5610432016-08-08 18:44:38 +01004043 PREPARE_FOR_EXECUTION_PRIMITIVE(context, Object, SetAccessor, bool);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004044 if (!Utils::OpenHandle(self)->IsJSObject()) return Just(false);
4045 i::Handle<i::JSObject> obj =
4046 i::Handle<i::JSObject>::cast(Utils::OpenHandle(self));
4047 v8::Local<AccessorSignature> signature;
4048 auto info = MakeAccessorInfo(name, getter, setter, data, settings, attributes,
Ben Murdoch097c5b22016-05-18 11:27:45 +01004049 signature, i::FLAG_disable_old_api_accessors);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004050 if (info.is_null()) return Nothing<bool>();
4051 bool fast = obj->HasFastProperties();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004052 i::Handle<i::Object> result;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004053 has_pending_exception =
4054 !i::JSObject::SetAccessor(obj, info).ToHandle(&result);
4055 RETURN_ON_FAILED_EXECUTION_PRIMITIVE(bool);
4056 if (result->IsUndefined()) return Nothing<bool>();
Emily Bernierd0a1eb72015-03-24 16:35:39 -04004057 if (fast) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004058 i::JSObject::MigrateSlowToFast(obj, 0, "APISetAccessor");
Emily Bernierd0a1eb72015-03-24 16:35:39 -04004059 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004060 return Just(true);
Steve Blocka7e24c12009-10-30 11:49:00 +00004061}
4062
4063
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004064Maybe<bool> Object::SetAccessor(Local<Context> context, Local<Name> name,
4065 AccessorNameGetterCallback getter,
4066 AccessorNameSetterCallback setter,
4067 MaybeLocal<Value> data, AccessControl settings,
4068 PropertyAttribute attribute) {
4069 return ObjectSetAccessor(context, this, name, getter, setter,
4070 data.FromMaybe(Local<Value>()), settings, attribute);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004071}
4072
4073
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004074bool Object::SetAccessor(Local<String> name, AccessorGetterCallback getter,
4075 AccessorSetterCallback setter, v8::Local<Value> data,
4076 AccessControl settings, PropertyAttribute attributes) {
4077 auto context = ContextFromHeapObject(Utils::OpenHandle(this));
4078 return ObjectSetAccessor(context, this, name, getter, setter, data, settings,
4079 attributes).FromMaybe(false);
4080}
4081
4082
4083bool Object::SetAccessor(Local<Name> name, AccessorNameGetterCallback getter,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004084 AccessorNameSetterCallback setter,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004085 v8::Local<Value> data, AccessControl settings,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004086 PropertyAttribute attributes) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004087 auto context = ContextFromHeapObject(Utils::OpenHandle(this));
4088 return ObjectSetAccessor(context, this, name, getter, setter, data, settings,
4089 attributes).FromMaybe(false);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004090}
4091
4092
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004093void Object::SetAccessorProperty(Local<Name> name, Local<Function> getter,
4094 Local<Function> setter,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004095 PropertyAttribute attribute,
4096 AccessControl settings) {
4097 // TODO(verwaest): Remove |settings|.
4098 DCHECK_EQ(v8::DEFAULT, settings);
Steve Block44f0eee2011-05-26 01:26:41 +01004099 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
Steve Block44f0eee2011-05-26 01:26:41 +01004100 ENTER_V8(isolate);
4101 i::HandleScope scope(isolate);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004102 auto self = Utils::OpenHandle(this);
4103 if (!self->IsJSObject()) return;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004104 i::Handle<i::Object> getter_i = v8::Utils::OpenHandle(*getter);
4105 i::Handle<i::Object> setter_i = v8::Utils::OpenHandle(*setter, true);
4106 if (setter_i.is_null()) setter_i = isolate->factory()->null_value();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004107 i::JSObject::DefineAccessor(i::Handle<i::JSObject>::cast(self),
4108 v8::Utils::OpenHandle(*name), getter_i, setter_i,
4109 static_cast<i::PropertyAttributes>(attribute));
Leon Clarkef7060e22010-06-03 12:02:55 +01004110}
4111
4112
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004113Maybe<bool> v8::Object::HasOwnProperty(Local<Context> context,
4114 Local<Name> key) {
Ben Murdochc5610432016-08-08 18:44:38 +01004115 PREPARE_FOR_EXECUTION_PRIMITIVE(context, Object, HasOwnProperty, bool);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004116 auto self = Utils::OpenHandle(this);
4117 auto key_val = Utils::OpenHandle(*key);
4118 auto result = i::JSReceiver::HasOwnProperty(self, key_val);
4119 has_pending_exception = result.IsNothing();
4120 RETURN_ON_FAILED_EXECUTION_PRIMITIVE(bool);
4121 return result;
Ben Murdoch257744e2011-11-30 15:57:28 +00004122}
4123
Ben Murdochc5610432016-08-08 18:44:38 +01004124Maybe<bool> v8::Object::HasOwnProperty(Local<Context> context, uint32_t index) {
4125 PREPARE_FOR_EXECUTION_PRIMITIVE(context, Object, HasOwnProperty, bool);
4126 auto self = Utils::OpenHandle(this);
4127 auto result = i::JSReceiver::HasOwnProperty(self, index);
4128 has_pending_exception = result.IsNothing();
4129 RETURN_ON_FAILED_EXECUTION_PRIMITIVE(bool);
4130 return result;
4131}
Ben Murdoch257744e2011-11-30 15:57:28 +00004132
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004133bool v8::Object::HasOwnProperty(Local<String> key) {
4134 auto context = ContextFromHeapObject(Utils::OpenHandle(this));
4135 return HasOwnProperty(context, key).FromMaybe(false);
4136}
4137
4138
4139Maybe<bool> v8::Object::HasRealNamedProperty(Local<Context> context,
4140 Local<Name> key) {
Ben Murdochc5610432016-08-08 18:44:38 +01004141 PREPARE_FOR_EXECUTION_PRIMITIVE(context, Object, HasRealNamedProperty, bool);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004142 auto self = Utils::OpenHandle(this);
4143 if (!self->IsJSObject()) return Just(false);
4144 auto key_val = Utils::OpenHandle(*key);
4145 auto result = i::JSObject::HasRealNamedProperty(
4146 i::Handle<i::JSObject>::cast(self), key_val);
4147 has_pending_exception = result.IsNothing();
4148 RETURN_ON_FAILED_EXECUTION_PRIMITIVE(bool);
4149 return result;
4150}
4151
4152
4153bool v8::Object::HasRealNamedProperty(Local<String> key) {
4154 auto context = ContextFromHeapObject(Utils::OpenHandle(this));
4155 return HasRealNamedProperty(context, key).FromMaybe(false);
4156}
4157
4158
4159Maybe<bool> v8::Object::HasRealIndexedProperty(Local<Context> context,
4160 uint32_t index) {
Ben Murdochc5610432016-08-08 18:44:38 +01004161 PREPARE_FOR_EXECUTION_PRIMITIVE(context, Object, HasRealIndexedProperty,
4162 bool);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004163 auto self = Utils::OpenHandle(this);
4164 if (!self->IsJSObject()) return Just(false);
4165 auto result = i::JSObject::HasRealElementProperty(
4166 i::Handle<i::JSObject>::cast(self), index);
4167 has_pending_exception = result.IsNothing();
4168 RETURN_ON_FAILED_EXECUTION_PRIMITIVE(bool);
4169 return result;
Steve Blocka7e24c12009-10-30 11:49:00 +00004170}
4171
4172
4173bool v8::Object::HasRealIndexedProperty(uint32_t index) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004174 auto context = ContextFromHeapObject(Utils::OpenHandle(this));
4175 return HasRealIndexedProperty(context, index).FromMaybe(false);
Steve Blocka7e24c12009-10-30 11:49:00 +00004176}
4177
4178
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004179Maybe<bool> v8::Object::HasRealNamedCallbackProperty(Local<Context> context,
4180 Local<Name> key) {
Ben Murdochc5610432016-08-08 18:44:38 +01004181 PREPARE_FOR_EXECUTION_PRIMITIVE(context, Object, HasRealNamedCallbackProperty,
4182 bool);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004183 auto self = Utils::OpenHandle(this);
4184 if (!self->IsJSObject()) return Just(false);
4185 auto key_val = Utils::OpenHandle(*key);
4186 auto result = i::JSObject::HasRealNamedCallbackProperty(
4187 i::Handle<i::JSObject>::cast(self), key_val);
4188 has_pending_exception = result.IsNothing();
4189 RETURN_ON_FAILED_EXECUTION_PRIMITIVE(bool);
4190 return result;
4191}
4192
4193
4194bool v8::Object::HasRealNamedCallbackProperty(Local<String> key) {
4195 auto context = ContextFromHeapObject(Utils::OpenHandle(this));
4196 return HasRealNamedCallbackProperty(context, key).FromMaybe(false);
Steve Blocka7e24c12009-10-30 11:49:00 +00004197}
4198
4199
4200bool v8::Object::HasNamedLookupInterceptor() {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004201 auto self = Utils::OpenHandle(this);
4202 return self->IsJSObject() &&
4203 i::Handle<i::JSObject>::cast(self)->HasNamedInterceptor();
Steve Blocka7e24c12009-10-30 11:49:00 +00004204}
4205
4206
4207bool v8::Object::HasIndexedLookupInterceptor() {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004208 auto self = Utils::OpenHandle(this);
4209 return self->IsJSObject() &&
4210 i::Handle<i::JSObject>::cast(self)->HasIndexedInterceptor();
Steve Blocka7e24c12009-10-30 11:49:00 +00004211}
4212
4213
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004214MaybeLocal<Value> v8::Object::GetRealNamedPropertyInPrototypeChain(
4215 Local<Context> context, Local<Name> key) {
Ben Murdochc5610432016-08-08 18:44:38 +01004216 PREPARE_FOR_EXECUTION(context, Object, GetRealNamedPropertyInPrototypeChain,
4217 Value);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004218 i::Handle<i::JSReceiver> self = Utils::OpenHandle(this);
4219 if (!self->IsJSObject()) return MaybeLocal<Value>();
4220 i::Handle<i::Name> key_obj = Utils::OpenHandle(*key);
4221 i::PrototypeIterator iter(isolate, self);
4222 if (iter.IsAtEnd()) return MaybeLocal<Value>();
4223 i::Handle<i::JSReceiver> proto =
4224 i::PrototypeIterator::GetCurrent<i::JSReceiver>(iter);
4225 i::LookupIterator it = i::LookupIterator::PropertyOrElement(
4226 isolate, self, key_obj, proto,
4227 i::LookupIterator::PROTOTYPE_CHAIN_SKIP_INTERCEPTOR);
4228 Local<Value> result;
4229 has_pending_exception = !ToLocal<Value>(i::Object::GetProperty(&it), &result);
4230 RETURN_ON_FAILED_EXECUTION(Value);
4231 if (!it.IsFound()) return MaybeLocal<Value>();
4232 RETURN_ESCAPED(result);
Ben Murdoch8b112d22011-06-08 16:22:53 +01004233}
4234
4235
Steve Blocka7e24c12009-10-30 11:49:00 +00004236Local<Value> v8::Object::GetRealNamedPropertyInPrototypeChain(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004237 Local<String> key) {
4238 auto context = ContextFromHeapObject(Utils::OpenHandle(this));
4239 RETURN_TO_LOCAL_UNCHECKED(GetRealNamedPropertyInPrototypeChain(context, key),
4240 Value);
Steve Blocka7e24c12009-10-30 11:49:00 +00004241}
4242
4243
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004244Maybe<PropertyAttribute>
4245v8::Object::GetRealNamedPropertyAttributesInPrototypeChain(
4246 Local<Context> context, Local<Name> key) {
4247 PREPARE_FOR_EXECUTION_PRIMITIVE(
Ben Murdochc5610432016-08-08 18:44:38 +01004248 context, Object, GetRealNamedPropertyAttributesInPrototypeChain,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004249 PropertyAttribute);
4250 i::Handle<i::JSReceiver> self = Utils::OpenHandle(this);
4251 if (!self->IsJSObject()) return Nothing<PropertyAttribute>();
4252 i::Handle<i::Name> key_obj = Utils::OpenHandle(*key);
4253 i::PrototypeIterator iter(isolate, self);
4254 if (iter.IsAtEnd()) return Nothing<PropertyAttribute>();
4255 i::Handle<i::JSReceiver> proto =
4256 i::PrototypeIterator::GetCurrent<i::JSReceiver>(iter);
4257 i::LookupIterator it = i::LookupIterator::PropertyOrElement(
4258 isolate, self, key_obj, proto,
4259 i::LookupIterator::PROTOTYPE_CHAIN_SKIP_INTERCEPTOR);
4260 Maybe<i::PropertyAttributes> result =
4261 i::JSReceiver::GetPropertyAttributes(&it);
4262 RETURN_ON_FAILED_EXECUTION_PRIMITIVE(PropertyAttribute);
4263 if (!it.IsFound()) return Nothing<PropertyAttribute>();
4264 if (result.FromJust() == i::ABSENT) return Just(None);
4265 return Just(static_cast<PropertyAttribute>(result.FromJust()));
Steve Blocka7e24c12009-10-30 11:49:00 +00004266}
4267
4268
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004269Maybe<PropertyAttribute>
4270v8::Object::GetRealNamedPropertyAttributesInPrototypeChain(Local<String> key) {
4271 auto context = ContextFromHeapObject(Utils::OpenHandle(this));
4272 return GetRealNamedPropertyAttributesInPrototypeChain(context, key);
4273}
Steve Blocka7e24c12009-10-30 11:49:00 +00004274
Ben Murdochb0fe1622011-05-05 13:52:32 +01004275
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004276MaybeLocal<Value> v8::Object::GetRealNamedProperty(Local<Context> context,
4277 Local<Name> key) {
Ben Murdochc5610432016-08-08 18:44:38 +01004278 PREPARE_FOR_EXECUTION(context, Object, GetRealNamedProperty, Value);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004279 auto self = Utils::OpenHandle(this);
4280 auto key_obj = Utils::OpenHandle(*key);
4281 i::LookupIterator it = i::LookupIterator::PropertyOrElement(
Ben Murdochda12d292016-06-02 14:46:10 +01004282 isolate, self, key_obj, self,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004283 i::LookupIterator::PROTOTYPE_CHAIN_SKIP_INTERCEPTOR);
4284 Local<Value> result;
4285 has_pending_exception = !ToLocal<Value>(i::Object::GetProperty(&it), &result);
4286 RETURN_ON_FAILED_EXECUTION(Value);
4287 if (!it.IsFound()) return MaybeLocal<Value>();
4288 RETURN_ESCAPED(result);
4289}
4290
4291
4292Local<Value> v8::Object::GetRealNamedProperty(Local<String> key) {
4293 auto context = ContextFromHeapObject(Utils::OpenHandle(this));
4294 RETURN_TO_LOCAL_UNCHECKED(GetRealNamedProperty(context, key), Value);
4295}
4296
4297
4298Maybe<PropertyAttribute> v8::Object::GetRealNamedPropertyAttributes(
4299 Local<Context> context, Local<Name> key) {
4300 PREPARE_FOR_EXECUTION_PRIMITIVE(
Ben Murdochc5610432016-08-08 18:44:38 +01004301 context, Object, GetRealNamedPropertyAttributes, PropertyAttribute);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004302 auto self = Utils::OpenHandle(this);
4303 auto key_obj = Utils::OpenHandle(*key);
4304 i::LookupIterator it = i::LookupIterator::PropertyOrElement(
Ben Murdochda12d292016-06-02 14:46:10 +01004305 isolate, self, key_obj, self,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004306 i::LookupIterator::PROTOTYPE_CHAIN_SKIP_INTERCEPTOR);
4307 auto result = i::JSReceiver::GetPropertyAttributes(&it);
4308 RETURN_ON_FAILED_EXECUTION_PRIMITIVE(PropertyAttribute);
4309 if (!it.IsFound()) return Nothing<PropertyAttribute>();
4310 if (result.FromJust() == i::ABSENT) {
4311 return Just(static_cast<PropertyAttribute>(i::NONE));
4312 }
4313 return Just<PropertyAttribute>(
4314 static_cast<PropertyAttribute>(result.FromJust()));
4315}
4316
4317
4318Maybe<PropertyAttribute> v8::Object::GetRealNamedPropertyAttributes(
4319 Local<String> key) {
4320 auto context = ContextFromHeapObject(Utils::OpenHandle(this));
4321 return GetRealNamedPropertyAttributes(context, key);
Steve Blocka7e24c12009-10-30 11:49:00 +00004322}
4323
4324
Steve Blocka7e24c12009-10-30 11:49:00 +00004325Local<v8::Object> v8::Object::Clone() {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004326 auto self = i::Handle<i::JSObject>::cast(Utils::OpenHandle(this));
4327 auto isolate = self->GetIsolate();
Steve Block44f0eee2011-05-26 01:26:41 +01004328 ENTER_V8(isolate);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004329 auto result = isolate->factory()->CopyJSObject(self);
4330 CHECK(!result.is_null());
Steve Blocka7e24c12009-10-30 11:49:00 +00004331 return Utils::ToLocal(result);
4332}
4333
4334
Ben Murdoch8b112d22011-06-08 16:22:53 +01004335Local<v8::Context> v8::Object::CreationContext() {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004336 auto self = Utils::OpenHandle(this);
4337 auto context = handle(self->GetCreationContext());
4338 return Utils::ToLocal(context);
Ben Murdoch8b112d22011-06-08 16:22:53 +01004339}
4340
4341
Steve Blocka7e24c12009-10-30 11:49:00 +00004342int v8::Object::GetIdentityHash() {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004343 auto isolate = Utils::OpenHandle(this)->GetIsolate();
Steve Block44f0eee2011-05-26 01:26:41 +01004344 i::HandleScope scope(isolate);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004345 auto self = Utils::OpenHandle(this);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004346 return i::JSReceiver::GetOrCreateIdentityHash(self)->value();
Steve Blocka7e24c12009-10-30 11:49:00 +00004347}
4348
4349
Ben Murdoch257744e2011-11-30 15:57:28 +00004350bool v8::Object::IsCallable() {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004351 auto self = Utils::OpenHandle(this);
4352 return self->IsCallable();
Ben Murdoch257744e2011-11-30 15:57:28 +00004353}
4354
Ben Murdochc5610432016-08-08 18:44:38 +01004355bool v8::Object::IsConstructor() {
4356 auto self = Utils::OpenHandle(this);
4357 return self->IsConstructor();
4358}
Ben Murdoch257744e2011-11-30 15:57:28 +00004359
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004360MaybeLocal<Value> Object::CallAsFunction(Local<Context> context,
4361 Local<Value> recv, int argc,
4362 Local<Value> argv[]) {
Ben Murdochc5610432016-08-08 18:44:38 +01004363 PREPARE_FOR_EXECUTION_WITH_CALLBACK(context, Object, CallAsFunction, Value);
4364 i::HistogramTimerScope execute_timer(isolate->counters()->execute(), true);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004365 i::TimerEventScope<i::TimerEventExecute> timer_scope(isolate);
Ben Murdoch097c5b22016-05-18 11:27:45 +01004366 TRACE_EVENT0("v8", "V8.Execute");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004367 auto self = Utils::OpenHandle(this);
4368 auto recv_obj = Utils::OpenHandle(*recv);
4369 STATIC_ASSERT(sizeof(v8::Local<v8::Value>) == sizeof(i::Object**));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004370 i::Handle<i::Object>* args = reinterpret_cast<i::Handle<i::Object>*>(argv);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004371 Local<Value> result;
4372 has_pending_exception = !ToLocal<Value>(
4373 i::Execution::Call(isolate, self, recv_obj, argc, args), &result);
4374 RETURN_ON_FAILED_EXECUTION(Value);
4375 RETURN_ESCAPED(result);
4376}
4377
4378
4379Local<v8::Value> Object::CallAsFunction(v8::Local<v8::Value> recv, int argc,
4380 v8::Local<v8::Value> argv[]) {
4381 auto context = ContextFromHeapObject(Utils::OpenHandle(this));
4382 Local<Value>* argv_cast = reinterpret_cast<Local<Value>*>(argv);
4383 RETURN_TO_LOCAL_UNCHECKED(CallAsFunction(context, recv, argc, argv_cast),
4384 Value);
4385}
4386
4387
4388MaybeLocal<Value> Object::CallAsConstructor(Local<Context> context, int argc,
4389 Local<Value> argv[]) {
Ben Murdochc5610432016-08-08 18:44:38 +01004390 PREPARE_FOR_EXECUTION_WITH_CALLBACK(context, Object, CallAsConstructor,
4391 Value);
4392 i::HistogramTimerScope execute_timer(isolate->counters()->execute(), true);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004393 i::TimerEventScope<i::TimerEventExecute> timer_scope(isolate);
Ben Murdoch097c5b22016-05-18 11:27:45 +01004394 TRACE_EVENT0("v8", "V8.Execute");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004395 auto self = Utils::OpenHandle(this);
4396 STATIC_ASSERT(sizeof(v8::Local<v8::Value>) == sizeof(i::Object**));
4397 i::Handle<i::Object>* args = reinterpret_cast<i::Handle<i::Object>*>(argv);
4398 Local<Value> result;
4399 has_pending_exception = !ToLocal<Value>(
4400 i::Execution::New(isolate, self, self, argc, args), &result);
4401 RETURN_ON_FAILED_EXECUTION(Value);
4402 RETURN_ESCAPED(result);
Ben Murdoch257744e2011-11-30 15:57:28 +00004403}
4404
4405
4406Local<v8::Value> Object::CallAsConstructor(int argc,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004407 v8::Local<v8::Value> argv[]) {
4408 auto context = ContextFromHeapObject(Utils::OpenHandle(this));
4409 Local<Value>* argv_cast = reinterpret_cast<Local<Value>*>(argv);
4410 RETURN_TO_LOCAL_UNCHECKED(CallAsConstructor(context, argc, argv_cast), Value);
Ben Murdoch257744e2011-11-30 15:57:28 +00004411}
4412
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004413MaybeLocal<Function> Function::New(Local<Context> context,
4414 FunctionCallback callback, Local<Value> data,
Ben Murdochc5610432016-08-08 18:44:38 +01004415 int length, ConstructorBehavior behavior) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004416 i::Isolate* isolate = Utils::OpenHandle(*context)->GetIsolate();
Ben Murdochc5610432016-08-08 18:44:38 +01004417 LOG_API(isolate, Function, New);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004418 ENTER_V8(isolate);
Ben Murdochc5610432016-08-08 18:44:38 +01004419 auto templ = FunctionTemplateNew(isolate, callback, nullptr, data,
4420 Local<Signature>(), length, true);
4421 if (behavior == ConstructorBehavior::kThrow) templ->RemovePrototype();
4422 return templ->GetFunction(context);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004423}
4424
4425
4426Local<Function> Function::New(Isolate* v8_isolate, FunctionCallback callback,
4427 Local<Value> data, int length) {
Ben Murdochc5610432016-08-08 18:44:38 +01004428 return Function::New(v8_isolate->GetCurrentContext(), callback, data, length,
4429 ConstructorBehavior::kAllow)
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004430 .FromMaybe(Local<Function>());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004431}
4432
4433
Steve Blocka7e24c12009-10-30 11:49:00 +00004434Local<v8::Object> Function::NewInstance() const {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004435 return NewInstance(Isolate::GetCurrent()->GetCurrentContext(), 0, NULL)
4436 .FromMaybe(Local<Object>());
4437}
4438
4439
4440MaybeLocal<Object> Function::NewInstance(Local<Context> context, int argc,
4441 v8::Local<v8::Value> argv[]) const {
Ben Murdochc5610432016-08-08 18:44:38 +01004442 PREPARE_FOR_EXECUTION_WITH_CALLBACK(context, Function, NewInstance, Object);
4443 i::HistogramTimerScope execute_timer(isolate->counters()->execute(), true);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004444 i::TimerEventScope<i::TimerEventExecute> timer_scope(isolate);
Ben Murdoch097c5b22016-05-18 11:27:45 +01004445 TRACE_EVENT0("v8", "V8.Execute");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004446 auto self = Utils::OpenHandle(this);
4447 STATIC_ASSERT(sizeof(v8::Local<v8::Value>) == sizeof(i::Object**));
4448 i::Handle<i::Object>* args = reinterpret_cast<i::Handle<i::Object>*>(argv);
4449 Local<Object> result;
4450 has_pending_exception = !ToLocal<Object>(
4451 i::Execution::New(isolate, self, self, argc, args), &result);
4452 RETURN_ON_FAILED_EXECUTION(Object);
4453 RETURN_ESCAPED(result);
Steve Blocka7e24c12009-10-30 11:49:00 +00004454}
4455
4456
4457Local<v8::Object> Function::NewInstance(int argc,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004458 v8::Local<v8::Value> argv[]) const {
4459 auto context = ContextFromHeapObject(Utils::OpenHandle(this));
4460 RETURN_TO_LOCAL_UNCHECKED(NewInstance(context, argc, argv), Object);
Steve Blocka7e24c12009-10-30 11:49:00 +00004461}
4462
4463
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004464MaybeLocal<v8::Value> Function::Call(Local<Context> context,
4465 v8::Local<v8::Value> recv, int argc,
4466 v8::Local<v8::Value> argv[]) {
Ben Murdochc5610432016-08-08 18:44:38 +01004467 PREPARE_FOR_EXECUTION_WITH_CALLBACK(context, Function, Call, Value);
4468 i::HistogramTimerScope execute_timer(isolate->counters()->execute(), true);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004469 i::TimerEventScope<i::TimerEventExecute> timer_scope(isolate);
Ben Murdoch097c5b22016-05-18 11:27:45 +01004470 TRACE_EVENT0("v8", "V8.Execute");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004471 auto self = Utils::OpenHandle(this);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004472 i::Handle<i::Object> recv_obj = Utils::OpenHandle(*recv);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004473 STATIC_ASSERT(sizeof(v8::Local<v8::Value>) == sizeof(i::Object**));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004474 i::Handle<i::Object>* args = reinterpret_cast<i::Handle<i::Object>*>(argv);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004475 Local<Value> result;
4476 has_pending_exception = !ToLocal<Value>(
4477 i::Execution::Call(isolate, self, recv_obj, argc, args), &result);
4478 RETURN_ON_FAILED_EXECUTION(Value);
4479 RETURN_ESCAPED(result);
Steve Blocka7e24c12009-10-30 11:49:00 +00004480}
4481
4482
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004483Local<v8::Value> Function::Call(v8::Local<v8::Value> recv, int argc,
4484 v8::Local<v8::Value> argv[]) {
4485 auto context = ContextFromHeapObject(Utils::OpenHandle(this));
4486 RETURN_TO_LOCAL_UNCHECKED(Call(context, recv, argc, argv), Value);
4487}
4488
4489
4490void Function::SetName(v8::Local<v8::String> name) {
4491 auto self = Utils::OpenHandle(this);
4492 if (!self->IsJSFunction()) return;
4493 auto func = i::Handle<i::JSFunction>::cast(self);
Steve Blocka7e24c12009-10-30 11:49:00 +00004494 func->shared()->set_name(*Utils::OpenHandle(*name));
4495}
4496
4497
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004498Local<Value> Function::GetName() const {
4499 auto self = Utils::OpenHandle(this);
Ben Murdochc5610432016-08-08 18:44:38 +01004500 i::Isolate* isolate = self->GetIsolate();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004501 if (self->IsJSBoundFunction()) {
4502 auto func = i::Handle<i::JSBoundFunction>::cast(self);
Ben Murdochc5610432016-08-08 18:44:38 +01004503 i::Handle<i::Object> name;
4504 ASSIGN_RETURN_ON_EXCEPTION_VALUE(isolate, name,
4505 i::JSBoundFunction::GetName(isolate, func),
4506 Local<Value>());
4507 return Utils::ToLocal(name);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004508 }
4509 if (self->IsJSFunction()) {
4510 auto func = i::Handle<i::JSFunction>::cast(self);
Ben Murdochc5610432016-08-08 18:44:38 +01004511 return Utils::ToLocal(handle(func->shared()->name(), isolate));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004512 }
Ben Murdochc5610432016-08-08 18:44:38 +01004513 return ToApiHandle<Primitive>(isolate->factory()->undefined_value());
Steve Blocka7e24c12009-10-30 11:49:00 +00004514}
4515
4516
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004517Local<Value> Function::GetInferredName() const {
4518 auto self = Utils::OpenHandle(this);
4519 if (!self->IsJSFunction()) {
4520 return ToApiHandle<Primitive>(
4521 self->GetIsolate()->factory()->undefined_value());
4522 }
4523 auto func = i::Handle<i::JSFunction>::cast(self);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004524 return Utils::ToLocal(i::Handle<i::Object>(func->shared()->inferred_name(),
4525 func->GetIsolate()));
4526}
4527
4528
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004529Local<Value> Function::GetDebugName() const {
4530 auto self = Utils::OpenHandle(this);
4531 if (!self->IsJSFunction()) {
4532 return ToApiHandle<Primitive>(
4533 self->GetIsolate()->factory()->undefined_value());
4534 }
4535 auto func = i::Handle<i::JSFunction>::cast(self);
4536 i::Handle<i::String> name = i::JSFunction::GetDebugName(func);
4537 return Utils::ToLocal(i::Handle<i::Object>(*name, name->GetIsolate()));
4538}
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004539
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004540
4541Local<Value> Function::GetDisplayName() const {
4542 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
4543 ENTER_V8(isolate);
4544 auto self = Utils::OpenHandle(this);
4545 if (!self->IsJSFunction()) {
4546 return ToApiHandle<Primitive>(isolate->factory()->undefined_value());
4547 }
4548 auto func = i::Handle<i::JSFunction>::cast(self);
4549 i::Handle<i::String> property_name =
4550 isolate->factory()->NewStringFromStaticChars("displayName");
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004551 i::Handle<i::Object> value =
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004552 i::JSReceiver::GetDataProperty(func, property_name);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004553 if (value->IsString()) {
4554 i::Handle<i::String> name = i::Handle<i::String>::cast(value);
4555 if (name->length() > 0) return Utils::ToLocal(name);
4556 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004557 return ToApiHandle<Primitive>(isolate->factory()->undefined_value());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004558}
4559
4560
Andrei Popescu402d9372010-02-26 13:31:12 +00004561ScriptOrigin Function::GetScriptOrigin() const {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004562 auto self = Utils::OpenHandle(this);
4563 if (!self->IsJSFunction()) {
4564 return v8::ScriptOrigin(Local<Value>());
4565 }
4566 auto func = i::Handle<i::JSFunction>::cast(self);
Andrei Popescu402d9372010-02-26 13:31:12 +00004567 if (func->shared()->script()->IsScript()) {
4568 i::Handle<i::Script> script(i::Script::cast(func->shared()->script()));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004569 return GetScriptOriginForScript(func->GetIsolate(), script);
Andrei Popescu402d9372010-02-26 13:31:12 +00004570 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004571 return v8::ScriptOrigin(Local<Value>());
Andrei Popescu402d9372010-02-26 13:31:12 +00004572}
4573
4574
4575const int Function::kLineOffsetNotFound = -1;
4576
4577
4578int Function::GetScriptLineNumber() const {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004579 auto self = Utils::OpenHandle(this);
4580 if (!self->IsJSFunction()) {
4581 return kLineOffsetNotFound;
4582 }
4583 auto func = i::Handle<i::JSFunction>::cast(self);
Andrei Popescu402d9372010-02-26 13:31:12 +00004584 if (func->shared()->script()->IsScript()) {
4585 i::Handle<i::Script> script(i::Script::cast(func->shared()->script()));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004586 return i::Script::GetLineNumber(script, func->shared()->start_position());
Andrei Popescu402d9372010-02-26 13:31:12 +00004587 }
4588 return kLineOffsetNotFound;
4589}
4590
4591
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004592int Function::GetScriptColumnNumber() const {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004593 auto self = Utils::OpenHandle(this);
4594 if (!self->IsJSFunction()) {
4595 return kLineOffsetNotFound;
4596 }
4597 auto func = i::Handle<i::JSFunction>::cast(self);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004598 if (func->shared()->script()->IsScript()) {
4599 i::Handle<i::Script> script(i::Script::cast(func->shared()->script()));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004600 return i::Script::GetColumnNumber(script, func->shared()->start_position());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004601 }
4602 return kLineOffsetNotFound;
4603}
4604
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004605
4606bool Function::IsBuiltin() const {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004607 auto self = Utils::OpenHandle(this);
4608 if (!self->IsJSFunction()) {
4609 return false;
4610 }
4611 auto func = i::Handle<i::JSFunction>::cast(self);
4612 return func->shared()->IsBuiltin();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004613}
4614
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004615
4616int Function::ScriptId() const {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004617 auto self = Utils::OpenHandle(this);
4618 if (!self->IsJSFunction()) {
4619 return v8::UnboundScript::kNoScriptId;
4620 }
4621 auto func = i::Handle<i::JSFunction>::cast(self);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004622 if (!func->shared()->script()->IsScript()) {
4623 return v8::UnboundScript::kNoScriptId;
4624 }
4625 i::Handle<i::Script> script(i::Script::cast(func->shared()->script()));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004626 return script->id();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004627}
4628
4629
4630Local<v8::Value> Function::GetBoundFunction() const {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004631 auto self = Utils::OpenHandle(this);
4632 if (self->IsJSBoundFunction()) {
4633 auto bound_function = i::Handle<i::JSBoundFunction>::cast(self);
4634 auto bound_target_function = i::handle(
4635 bound_function->bound_target_function(), bound_function->GetIsolate());
4636 return Utils::CallableToLocal(bound_target_function);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004637 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004638 return v8::Undefined(reinterpret_cast<v8::Isolate*>(self->GetIsolate()));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004639}
4640
4641
Emily Bernierd0a1eb72015-03-24 16:35:39 -04004642int Name::GetIdentityHash() {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004643 auto self = Utils::OpenHandle(this);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04004644 return static_cast<int>(self->Hash());
4645}
4646
4647
Steve Blocka7e24c12009-10-30 11:49:00 +00004648int String::Length() const {
Steve Block44f0eee2011-05-26 01:26:41 +01004649 i::Handle<i::String> str = Utils::OpenHandle(this);
Steve Block44f0eee2011-05-26 01:26:41 +01004650 return str->length();
Steve Blocka7e24c12009-10-30 11:49:00 +00004651}
4652
4653
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004654bool String::IsOneByte() const {
4655 i::Handle<i::String> str = Utils::OpenHandle(this);
4656 return str->HasOnlyOneByteChars();
4657}
4658
4659
4660// Helpers for ContainsOnlyOneByteHelper
4661template<size_t size> struct OneByteMask;
4662template<> struct OneByteMask<4> {
4663 static const uint32_t value = 0xFF00FF00;
4664};
4665template<> struct OneByteMask<8> {
4666 static const uint64_t value = V8_2PART_UINT64_C(0xFF00FF00, FF00FF00);
4667};
4668static const uintptr_t kOneByteMask = OneByteMask<sizeof(uintptr_t)>::value;
4669static const uintptr_t kAlignmentMask = sizeof(uintptr_t) - 1;
4670static inline bool Unaligned(const uint16_t* chars) {
4671 return reinterpret_cast<const uintptr_t>(chars) & kAlignmentMask;
4672}
4673
4674
4675static inline const uint16_t* Align(const uint16_t* chars) {
4676 return reinterpret_cast<uint16_t*>(
4677 reinterpret_cast<uintptr_t>(chars) & ~kAlignmentMask);
4678}
4679
4680class ContainsOnlyOneByteHelper {
4681 public:
4682 ContainsOnlyOneByteHelper() : is_one_byte_(true) {}
4683 bool Check(i::String* string) {
4684 i::ConsString* cons_string = i::String::VisitFlat(this, string, 0);
4685 if (cons_string == NULL) return is_one_byte_;
4686 return CheckCons(cons_string);
4687 }
4688 void VisitOneByteString(const uint8_t* chars, int length) {
4689 // Nothing to do.
4690 }
4691 void VisitTwoByteString(const uint16_t* chars, int length) {
4692 // Accumulated bits.
4693 uintptr_t acc = 0;
4694 // Align to uintptr_t.
4695 const uint16_t* end = chars + length;
4696 while (Unaligned(chars) && chars != end) {
4697 acc |= *chars++;
4698 }
4699 // Read word aligned in blocks,
4700 // checking the return value at the end of each block.
4701 const uint16_t* aligned_end = Align(end);
4702 const int increment = sizeof(uintptr_t)/sizeof(uint16_t);
4703 const int inner_loops = 16;
4704 while (chars + inner_loops*increment < aligned_end) {
4705 for (int i = 0; i < inner_loops; i++) {
4706 acc |= *reinterpret_cast<const uintptr_t*>(chars);
4707 chars += increment;
4708 }
4709 // Check for early return.
4710 if ((acc & kOneByteMask) != 0) {
4711 is_one_byte_ = false;
4712 return;
4713 }
4714 }
4715 // Read the rest.
4716 while (chars != end) {
4717 acc |= *chars++;
4718 }
4719 // Check result.
4720 if ((acc & kOneByteMask) != 0) is_one_byte_ = false;
4721 }
4722
4723 private:
4724 bool CheckCons(i::ConsString* cons_string) {
4725 while (true) {
4726 // Check left side if flat.
4727 i::String* left = cons_string->first();
4728 i::ConsString* left_as_cons =
4729 i::String::VisitFlat(this, left, 0);
4730 if (!is_one_byte_) return false;
4731 // Check right side if flat.
4732 i::String* right = cons_string->second();
4733 i::ConsString* right_as_cons =
4734 i::String::VisitFlat(this, right, 0);
4735 if (!is_one_byte_) return false;
4736 // Standard recurse/iterate trick.
4737 if (left_as_cons != NULL && right_as_cons != NULL) {
4738 if (left->length() < right->length()) {
4739 CheckCons(left_as_cons);
4740 cons_string = right_as_cons;
4741 } else {
4742 CheckCons(right_as_cons);
4743 cons_string = left_as_cons;
4744 }
4745 // Check fast return.
4746 if (!is_one_byte_) return false;
4747 continue;
4748 }
4749 // Descend left in place.
4750 if (left_as_cons != NULL) {
4751 cons_string = left_as_cons;
4752 continue;
4753 }
4754 // Descend right in place.
4755 if (right_as_cons != NULL) {
4756 cons_string = right_as_cons;
4757 continue;
4758 }
4759 // Terminate.
4760 break;
4761 }
4762 return is_one_byte_;
4763 }
4764 bool is_one_byte_;
4765 DISALLOW_COPY_AND_ASSIGN(ContainsOnlyOneByteHelper);
4766};
4767
4768
4769bool String::ContainsOnlyOneByte() const {
4770 i::Handle<i::String> str = Utils::OpenHandle(this);
4771 if (str->HasOnlyOneByteChars()) return true;
4772 ContainsOnlyOneByteHelper helper;
4773 return helper.Check(*str);
4774}
4775
4776
4777class Utf8LengthHelper : public i::AllStatic {
4778 public:
4779 enum State {
4780 kEndsWithLeadingSurrogate = 1 << 0,
4781 kStartsWithTrailingSurrogate = 1 << 1,
4782 kLeftmostEdgeIsCalculated = 1 << 2,
4783 kRightmostEdgeIsCalculated = 1 << 3,
4784 kLeftmostEdgeIsSurrogate = 1 << 4,
4785 kRightmostEdgeIsSurrogate = 1 << 5
4786 };
4787
4788 static const uint8_t kInitialState = 0;
4789
4790 static inline bool EndsWithSurrogate(uint8_t state) {
4791 return state & kEndsWithLeadingSurrogate;
4792 }
4793
4794 static inline bool StartsWithSurrogate(uint8_t state) {
4795 return state & kStartsWithTrailingSurrogate;
4796 }
4797
4798 class Visitor {
4799 public:
4800 Visitor() : utf8_length_(0), state_(kInitialState) {}
4801
4802 void VisitOneByteString(const uint8_t* chars, int length) {
4803 int utf8_length = 0;
4804 // Add in length 1 for each non-Latin1 character.
4805 for (int i = 0; i < length; i++) {
4806 utf8_length += *chars++ >> 7;
4807 }
4808 // Add in length 1 for each character.
4809 utf8_length_ = utf8_length + length;
4810 state_ = kInitialState;
4811 }
4812
4813 void VisitTwoByteString(const uint16_t* chars, int length) {
4814 int utf8_length = 0;
4815 int last_character = unibrow::Utf16::kNoPreviousCharacter;
4816 for (int i = 0; i < length; i++) {
4817 uint16_t c = chars[i];
4818 utf8_length += unibrow::Utf8::Length(c, last_character);
4819 last_character = c;
4820 }
4821 utf8_length_ = utf8_length;
4822 uint8_t state = 0;
4823 if (unibrow::Utf16::IsTrailSurrogate(chars[0])) {
4824 state |= kStartsWithTrailingSurrogate;
4825 }
4826 if (unibrow::Utf16::IsLeadSurrogate(chars[length-1])) {
4827 state |= kEndsWithLeadingSurrogate;
4828 }
4829 state_ = state;
4830 }
4831
4832 static i::ConsString* VisitFlat(i::String* string,
4833 int* length,
4834 uint8_t* state) {
4835 Visitor visitor;
4836 i::ConsString* cons_string = i::String::VisitFlat(&visitor, string);
4837 *length = visitor.utf8_length_;
4838 *state = visitor.state_;
4839 return cons_string;
4840 }
4841
4842 private:
4843 int utf8_length_;
4844 uint8_t state_;
4845 DISALLOW_COPY_AND_ASSIGN(Visitor);
4846 };
4847
4848 static inline void MergeLeafLeft(int* length,
4849 uint8_t* state,
4850 uint8_t leaf_state) {
4851 bool edge_surrogate = StartsWithSurrogate(leaf_state);
4852 if (!(*state & kLeftmostEdgeIsCalculated)) {
4853 DCHECK(!(*state & kLeftmostEdgeIsSurrogate));
4854 *state |= kLeftmostEdgeIsCalculated
4855 | (edge_surrogate ? kLeftmostEdgeIsSurrogate : 0);
4856 } else if (EndsWithSurrogate(*state) && edge_surrogate) {
4857 *length -= unibrow::Utf8::kBytesSavedByCombiningSurrogates;
4858 }
4859 if (EndsWithSurrogate(leaf_state)) {
4860 *state |= kEndsWithLeadingSurrogate;
4861 } else {
4862 *state &= ~kEndsWithLeadingSurrogate;
4863 }
4864 }
4865
4866 static inline void MergeLeafRight(int* length,
4867 uint8_t* state,
4868 uint8_t leaf_state) {
4869 bool edge_surrogate = EndsWithSurrogate(leaf_state);
4870 if (!(*state & kRightmostEdgeIsCalculated)) {
4871 DCHECK(!(*state & kRightmostEdgeIsSurrogate));
4872 *state |= (kRightmostEdgeIsCalculated
4873 | (edge_surrogate ? kRightmostEdgeIsSurrogate : 0));
4874 } else if (edge_surrogate && StartsWithSurrogate(*state)) {
4875 *length -= unibrow::Utf8::kBytesSavedByCombiningSurrogates;
4876 }
4877 if (StartsWithSurrogate(leaf_state)) {
4878 *state |= kStartsWithTrailingSurrogate;
4879 } else {
4880 *state &= ~kStartsWithTrailingSurrogate;
4881 }
4882 }
4883
4884 static inline void MergeTerminal(int* length,
4885 uint8_t state,
4886 uint8_t* state_out) {
4887 DCHECK((state & kLeftmostEdgeIsCalculated) &&
4888 (state & kRightmostEdgeIsCalculated));
4889 if (EndsWithSurrogate(state) && StartsWithSurrogate(state)) {
4890 *length -= unibrow::Utf8::kBytesSavedByCombiningSurrogates;
4891 }
4892 *state_out = kInitialState |
4893 (state & kLeftmostEdgeIsSurrogate ? kStartsWithTrailingSurrogate : 0) |
4894 (state & kRightmostEdgeIsSurrogate ? kEndsWithLeadingSurrogate : 0);
4895 }
4896
4897 static int Calculate(i::ConsString* current, uint8_t* state_out) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004898 using internal::ConsString;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004899 int total_length = 0;
4900 uint8_t state = kInitialState;
4901 while (true) {
4902 i::String* left = current->first();
4903 i::String* right = current->second();
4904 uint8_t right_leaf_state;
4905 uint8_t left_leaf_state;
4906 int leaf_length;
4907 ConsString* left_as_cons =
4908 Visitor::VisitFlat(left, &leaf_length, &left_leaf_state);
4909 if (left_as_cons == NULL) {
4910 total_length += leaf_length;
4911 MergeLeafLeft(&total_length, &state, left_leaf_state);
4912 }
4913 ConsString* right_as_cons =
4914 Visitor::VisitFlat(right, &leaf_length, &right_leaf_state);
4915 if (right_as_cons == NULL) {
4916 total_length += leaf_length;
4917 MergeLeafRight(&total_length, &state, right_leaf_state);
4918 if (left_as_cons != NULL) {
4919 // 1 Leaf node. Descend in place.
4920 current = left_as_cons;
4921 continue;
4922 } else {
4923 // Terminal node.
4924 MergeTerminal(&total_length, state, state_out);
4925 return total_length;
4926 }
4927 } else if (left_as_cons == NULL) {
4928 // 1 Leaf node. Descend in place.
4929 current = right_as_cons;
4930 continue;
4931 }
4932 // Both strings are ConsStrings.
4933 // Recurse on smallest.
4934 if (left->length() < right->length()) {
4935 total_length += Calculate(left_as_cons, &left_leaf_state);
4936 MergeLeafLeft(&total_length, &state, left_leaf_state);
4937 current = right_as_cons;
4938 } else {
4939 total_length += Calculate(right_as_cons, &right_leaf_state);
4940 MergeLeafRight(&total_length, &state, right_leaf_state);
4941 current = left_as_cons;
4942 }
4943 }
4944 UNREACHABLE();
4945 return 0;
4946 }
4947
4948 static inline int Calculate(i::ConsString* current) {
4949 uint8_t state = kInitialState;
4950 return Calculate(current, &state);
4951 }
4952
4953 private:
4954 DISALLOW_IMPLICIT_CONSTRUCTORS(Utf8LengthHelper);
4955};
4956
4957
4958static int Utf8Length(i::String* str, i::Isolate* isolate) {
4959 int length = str->length();
4960 if (length == 0) return 0;
4961 uint8_t state;
4962 i::ConsString* cons_string =
4963 Utf8LengthHelper::Visitor::VisitFlat(str, &length, &state);
4964 if (cons_string == NULL) return length;
4965 return Utf8LengthHelper::Calculate(cons_string);
4966}
4967
4968
Steve Blocka7e24c12009-10-30 11:49:00 +00004969int String::Utf8Length() const {
Steve Block44f0eee2011-05-26 01:26:41 +01004970 i::Handle<i::String> str = Utils::OpenHandle(this);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004971 i::Isolate* isolate = str->GetIsolate();
4972 return v8::Utf8Length(*str, isolate);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004973}
4974
4975
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004976class Utf8WriterVisitor {
4977 public:
4978 Utf8WriterVisitor(
4979 char* buffer,
4980 int capacity,
4981 bool skip_capacity_check,
4982 bool replace_invalid_utf8)
4983 : early_termination_(false),
4984 last_character_(unibrow::Utf16::kNoPreviousCharacter),
4985 buffer_(buffer),
4986 start_(buffer),
4987 capacity_(capacity),
4988 skip_capacity_check_(capacity == -1 || skip_capacity_check),
4989 replace_invalid_utf8_(replace_invalid_utf8),
4990 utf16_chars_read_(0) {
4991 }
4992
4993 static int WriteEndCharacter(uint16_t character,
4994 int last_character,
4995 int remaining,
4996 char* const buffer,
4997 bool replace_invalid_utf8) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004998 DCHECK_GT(remaining, 0);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004999 // We can't use a local buffer here because Encode needs to modify
5000 // previous characters in the stream. We know, however, that
5001 // exactly one character will be advanced.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005002 if (unibrow::Utf16::IsSurrogatePair(last_character, character)) {
5003 int written = unibrow::Utf8::Encode(buffer, character, last_character,
5004 replace_invalid_utf8);
5005 DCHECK_EQ(written, 1);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005006 return written;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005007 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005008 // Use a scratch buffer to check the required characters.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005009 char temp_buffer[unibrow::Utf8::kMaxEncodedSize];
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005010 // Can't encode using last_character as gcc has array bounds issues.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005011 int written = unibrow::Utf8::Encode(temp_buffer, character,
5012 unibrow::Utf16::kNoPreviousCharacter,
5013 replace_invalid_utf8);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005014 // Won't fit.
5015 if (written > remaining) return 0;
5016 // Copy over the character from temp_buffer.
5017 for (int j = 0; j < written; j++) {
5018 buffer[j] = temp_buffer[j];
5019 }
5020 return written;
5021 }
5022
5023 // Visit writes out a group of code units (chars) of a v8::String to the
5024 // internal buffer_. This is done in two phases. The first phase calculates a
5025 // pesimistic estimate (writable_length) on how many code units can be safely
5026 // written without exceeding the buffer capacity and without writing the last
5027 // code unit (it could be a lead surrogate). The estimated number of code
5028 // units is then written out in one go, and the reported byte usage is used
5029 // to correct the estimate. This is repeated until the estimate becomes <= 0
5030 // or all code units have been written out. The second phase writes out code
5031 // units until the buffer capacity is reached, would be exceeded by the next
5032 // unit, or all units have been written out.
5033 template<typename Char>
5034 void Visit(const Char* chars, const int length) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005035 DCHECK(!early_termination_);
5036 if (length == 0) return;
5037 // Copy state to stack.
5038 char* buffer = buffer_;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005039 int last_character = sizeof(Char) == 1
5040 ? unibrow::Utf16::kNoPreviousCharacter
5041 : last_character_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005042 int i = 0;
5043 // Do a fast loop where there is no exit capacity check.
5044 while (true) {
5045 int fast_length;
5046 if (skip_capacity_check_) {
5047 fast_length = length;
5048 } else {
5049 int remaining_capacity = capacity_ - static_cast<int>(buffer - start_);
5050 // Need enough space to write everything but one character.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005051 STATIC_ASSERT(unibrow::Utf16::kMaxExtraUtf8BytesForOneUtf16CodeUnit ==
5052 3);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005053 int max_size_per_char = sizeof(Char) == 1 ? 2 : 3;
5054 int writable_length =
5055 (remaining_capacity - max_size_per_char)/max_size_per_char;
5056 // Need to drop into slow loop.
5057 if (writable_length <= 0) break;
5058 fast_length = i + writable_length;
5059 if (fast_length > length) fast_length = length;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005060 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005061 // Write the characters to the stream.
5062 if (sizeof(Char) == 1) {
5063 for (; i < fast_length; i++) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005064 buffer += unibrow::Utf8::EncodeOneByte(
5065 buffer, static_cast<uint8_t>(*chars++));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005066 DCHECK(capacity_ == -1 || (buffer - start_) <= capacity_);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005067 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005068 } else {
5069 for (; i < fast_length; i++) {
5070 uint16_t character = *chars++;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005071 buffer += unibrow::Utf8::Encode(buffer, character, last_character,
5072 replace_invalid_utf8_);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005073 last_character = character;
5074 DCHECK(capacity_ == -1 || (buffer - start_) <= capacity_);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005075 }
5076 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005077 // Array is fully written. Exit.
5078 if (fast_length == length) {
5079 // Write state back out to object.
5080 last_character_ = last_character;
5081 buffer_ = buffer;
5082 utf16_chars_read_ += length;
5083 return;
5084 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005085 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005086 DCHECK(!skip_capacity_check_);
5087 // Slow loop. Must check capacity on each iteration.
5088 int remaining_capacity = capacity_ - static_cast<int>(buffer - start_);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005089 DCHECK_GE(remaining_capacity, 0);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005090 for (; i < length && remaining_capacity > 0; i++) {
5091 uint16_t character = *chars++;
5092 // remaining_capacity is <= 3 bytes at this point, so we do not write out
5093 // an umatched lead surrogate.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005094 if (replace_invalid_utf8_ && unibrow::Utf16::IsLeadSurrogate(character)) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005095 early_termination_ = true;
5096 break;
5097 }
5098 int written = WriteEndCharacter(character,
5099 last_character,
5100 remaining_capacity,
5101 buffer,
5102 replace_invalid_utf8_);
5103 if (written == 0) {
5104 early_termination_ = true;
5105 break;
5106 }
5107 buffer += written;
5108 remaining_capacity -= written;
5109 last_character = character;
5110 }
5111 // Write state back out to object.
5112 last_character_ = last_character;
5113 buffer_ = buffer;
5114 utf16_chars_read_ += i;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005115 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005116
5117 inline bool IsDone() {
5118 return early_termination_;
5119 }
5120
5121 inline void VisitOneByteString(const uint8_t* chars, int length) {
5122 Visit(chars, length);
5123 }
5124
5125 inline void VisitTwoByteString(const uint16_t* chars, int length) {
5126 Visit(chars, length);
5127 }
5128
5129 int CompleteWrite(bool write_null, int* utf16_chars_read_out) {
5130 // Write out number of utf16 characters written to the stream.
5131 if (utf16_chars_read_out != NULL) {
5132 *utf16_chars_read_out = utf16_chars_read_;
5133 }
5134 // Only null terminate if all of the string was written and there's space.
5135 if (write_null &&
5136 !early_termination_ &&
5137 (capacity_ == -1 || (buffer_ - start_) < capacity_)) {
5138 *buffer_++ = '\0';
5139 }
5140 return static_cast<int>(buffer_ - start_);
5141 }
5142
5143 private:
5144 bool early_termination_;
5145 int last_character_;
5146 char* buffer_;
5147 char* const start_;
5148 int capacity_;
5149 bool const skip_capacity_check_;
5150 bool const replace_invalid_utf8_;
5151 int utf16_chars_read_;
5152 DISALLOW_IMPLICIT_CONSTRUCTORS(Utf8WriterVisitor);
5153};
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005154
5155
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005156static bool RecursivelySerializeToUtf8(i::String* current,
5157 Utf8WriterVisitor* writer,
5158 int recursion_budget) {
5159 while (!writer->IsDone()) {
5160 i::ConsString* cons_string = i::String::VisitFlat(writer, current);
5161 if (cons_string == NULL) return true; // Leaf node.
5162 if (recursion_budget <= 0) return false;
5163 // Must write the left branch first.
5164 i::String* first = cons_string->first();
5165 bool success = RecursivelySerializeToUtf8(first,
5166 writer,
5167 recursion_budget - 1);
5168 if (!success) return false;
5169 // Inline tail recurse for right branch.
5170 current = cons_string->second();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005171 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005172 return true;
Steve Blocka7e24c12009-10-30 11:49:00 +00005173}
5174
5175
Steve Block6ded16b2010-05-10 14:33:55 +01005176int String::WriteUtf8(char* buffer,
5177 int capacity,
5178 int* nchars_ref,
Ben Murdoch69a99ed2011-11-30 16:03:39 +00005179 int options) const {
Steve Block44f0eee2011-05-26 01:26:41 +01005180 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
Ben Murdochc5610432016-08-08 18:44:38 +01005181 LOG_API(isolate, String, WriteUtf8);
Steve Block44f0eee2011-05-26 01:26:41 +01005182 ENTER_V8(isolate);
Ben Murdoch85b71792012-04-11 18:30:58 +01005183 i::Handle<i::String> str = Utils::OpenHandle(this);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005184 if (options & HINT_MANY_WRITES_EXPECTED) {
5185 str = i::String::Flatten(str); // Flatten the string for efficiency.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005186 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005187 const int string_length = str->length();
5188 bool write_null = !(options & NO_NULL_TERMINATION);
5189 bool replace_invalid_utf8 = (options & REPLACE_INVALID_UTF8);
5190 int max16BitCodeUnitSize = unibrow::Utf8::kMax16BitCodeUnitSize;
5191 // First check if we can just write the string without checking capacity.
5192 if (capacity == -1 || capacity / max16BitCodeUnitSize >= string_length) {
5193 Utf8WriterVisitor writer(buffer, capacity, true, replace_invalid_utf8);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005194 const int kMaxRecursion = 100;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005195 bool success = RecursivelySerializeToUtf8(*str, &writer, kMaxRecursion);
5196 if (success) return writer.CompleteWrite(write_null, nchars_ref);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005197 } else if (capacity >= string_length) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005198 // First check that the buffer is large enough.
5199 int utf8_bytes = v8::Utf8Length(*str, str->GetIsolate());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005200 if (utf8_bytes <= capacity) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005201 // one-byte fast path.
5202 if (utf8_bytes == string_length) {
5203 WriteOneByte(reinterpret_cast<uint8_t*>(buffer), 0, capacity, options);
5204 if (nchars_ref != NULL) *nchars_ref = string_length;
5205 if (write_null && (utf8_bytes+1 <= capacity)) {
5206 return string_length + 1;
5207 }
5208 return string_length;
5209 }
5210 if (write_null && (utf8_bytes+1 > capacity)) {
5211 options |= NO_NULL_TERMINATION;
5212 }
5213 // Recurse once without a capacity limit.
5214 // This will get into the first branch above.
5215 // TODO(dcarney) Check max left rec. in Utf8Length and fall through.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005216 return WriteUtf8(buffer, -1, nchars_ref, options);
5217 }
5218 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005219 // Recursive slow path can potentially be unreasonable slow. Flatten.
5220 str = i::String::Flatten(str);
5221 Utf8WriterVisitor writer(buffer, capacity, false, replace_invalid_utf8);
5222 i::String::VisitFlat(&writer, *str);
5223 return writer.CompleteWrite(write_null, nchars_ref);
Steve Blocka7e24c12009-10-30 11:49:00 +00005224}
5225
5226
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005227template<typename CharType>
5228static inline int WriteHelper(const String* string,
5229 CharType* buffer,
5230 int start,
5231 int length,
5232 int options) {
5233 i::Isolate* isolate = Utils::OpenHandle(string)->GetIsolate();
Ben Murdochc5610432016-08-08 18:44:38 +01005234 LOG_API(isolate, String, Write);
Steve Block44f0eee2011-05-26 01:26:41 +01005235 ENTER_V8(isolate);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005236 DCHECK(start >= 0 && length >= -1);
5237 i::Handle<i::String> str = Utils::OpenHandle(string);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005238 if (options & String::HINT_MANY_WRITES_EXPECTED) {
Steve Block6ded16b2010-05-10 14:33:55 +01005239 // Flatten the string for efficiency. This applies whether we are
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005240 // using StringCharacterStream or Get(i) to access the characters.
5241 str = i::String::Flatten(str);
Steve Block6ded16b2010-05-10 14:33:55 +01005242 }
Ben Murdochb0fe1622011-05-05 13:52:32 +01005243 int end = start + length;
5244 if ((length == -1) || (length > str->length() - start) )
5245 end = str->length();
Steve Blocka7e24c12009-10-30 11:49:00 +00005246 if (end < 0) return 0;
5247 i::String::WriteToFlat(*str, buffer, start, end);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005248 if (!(options & String::NO_NULL_TERMINATION) &&
Ben Murdoch69a99ed2011-11-30 16:03:39 +00005249 (length == -1 || end - start < length)) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01005250 buffer[end - start] = '\0';
5251 }
5252 return end - start;
Steve Blocka7e24c12009-10-30 11:49:00 +00005253}
5254
5255
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005256int String::WriteOneByte(uint8_t* buffer,
5257 int start,
5258 int length,
5259 int options) const {
5260 return WriteHelper(this, buffer, start, length, options);
5261}
5262
5263
5264int String::Write(uint16_t* buffer,
5265 int start,
5266 int length,
5267 int options) const {
5268 return WriteHelper(this, buffer, start, length, options);
5269}
5270
5271
Steve Blocka7e24c12009-10-30 11:49:00 +00005272bool v8::String::IsExternal() const {
Steve Blocka7e24c12009-10-30 11:49:00 +00005273 i::Handle<i::String> str = Utils::OpenHandle(this);
5274 return i::StringShape(*str).IsExternalTwoByte();
5275}
5276
5277
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005278bool v8::String::IsExternalOneByte() const {
Steve Blocka7e24c12009-10-30 11:49:00 +00005279 i::Handle<i::String> str = Utils::OpenHandle(this);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005280 return i::StringShape(*str).IsExternalOneByte();
Steve Blocka7e24c12009-10-30 11:49:00 +00005281}
5282
5283
5284void v8::String::VerifyExternalStringResource(
5285 v8::String::ExternalStringResource* value) const {
5286 i::Handle<i::String> str = Utils::OpenHandle(this);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005287 const v8::String::ExternalStringResource* expected;
Steve Blocka7e24c12009-10-30 11:49:00 +00005288 if (i::StringShape(*str).IsExternalTwoByte()) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005289 const void* resource =
5290 i::Handle<i::ExternalTwoByteString>::cast(str)->resource();
5291 expected = reinterpret_cast<const ExternalStringResource*>(resource);
Steve Blocka7e24c12009-10-30 11:49:00 +00005292 } else {
5293 expected = NULL;
5294 }
5295 CHECK_EQ(expected, value);
5296}
5297
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005298void v8::String::VerifyExternalStringResourceBase(
5299 v8::String::ExternalStringResourceBase* value, Encoding encoding) const {
Steve Blocka7e24c12009-10-30 11:49:00 +00005300 i::Handle<i::String> str = Utils::OpenHandle(this);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005301 const v8::String::ExternalStringResourceBase* expected;
5302 Encoding expectedEncoding;
5303 if (i::StringShape(*str).IsExternalOneByte()) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005304 const void* resource =
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005305 i::Handle<i::ExternalOneByteString>::cast(str)->resource();
5306 expected = reinterpret_cast<const ExternalStringResourceBase*>(resource);
5307 expectedEncoding = ONE_BYTE_ENCODING;
5308 } else if (i::StringShape(*str).IsExternalTwoByte()) {
5309 const void* resource =
5310 i::Handle<i::ExternalTwoByteString>::cast(str)->resource();
5311 expected = reinterpret_cast<const ExternalStringResourceBase*>(resource);
5312 expectedEncoding = TWO_BYTE_ENCODING;
5313 } else {
5314 expected = NULL;
5315 expectedEncoding =
5316 str->IsOneByteRepresentation() ? ONE_BYTE_ENCODING : TWO_BYTE_ENCODING;
5317 }
5318 CHECK_EQ(expected, value);
5319 CHECK_EQ(expectedEncoding, encoding);
5320}
5321
5322const v8::String::ExternalOneByteStringResource*
5323v8::String::GetExternalOneByteStringResource() const {
5324 i::Handle<i::String> str = Utils::OpenHandle(this);
5325 if (i::StringShape(*str).IsExternalOneByte()) {
5326 const void* resource =
5327 i::Handle<i::ExternalOneByteString>::cast(str)->resource();
5328 return reinterpret_cast<const ExternalOneByteStringResource*>(resource);
Steve Blocka7e24c12009-10-30 11:49:00 +00005329 } else {
5330 return NULL;
5331 }
5332}
5333
5334
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005335Local<Value> Symbol::Name() const {
5336 i::Handle<i::Symbol> sym = Utils::OpenHandle(this);
5337 i::Handle<i::Object> name(sym->name(), sym->GetIsolate());
5338 return Utils::ToLocal(name);
5339}
5340
5341
5342Local<Value> Private::Name() const {
5343 return reinterpret_cast<const Symbol*>(this)->Name();
5344}
5345
5346
Steve Blocka7e24c12009-10-30 11:49:00 +00005347double Number::Value() const {
Steve Blocka7e24c12009-10-30 11:49:00 +00005348 i::Handle<i::Object> obj = Utils::OpenHandle(this);
5349 return obj->Number();
5350}
5351
5352
5353bool Boolean::Value() const {
Steve Blocka7e24c12009-10-30 11:49:00 +00005354 i::Handle<i::Object> obj = Utils::OpenHandle(this);
5355 return obj->IsTrue();
5356}
5357
5358
5359int64_t Integer::Value() const {
Steve Blocka7e24c12009-10-30 11:49:00 +00005360 i::Handle<i::Object> obj = Utils::OpenHandle(this);
5361 if (obj->IsSmi()) {
5362 return i::Smi::cast(*obj)->value();
5363 } else {
5364 return static_cast<int64_t>(obj->Number());
5365 }
5366}
5367
5368
5369int32_t Int32::Value() const {
Steve Blocka7e24c12009-10-30 11:49:00 +00005370 i::Handle<i::Object> obj = Utils::OpenHandle(this);
5371 if (obj->IsSmi()) {
5372 return i::Smi::cast(*obj)->value();
5373 } else {
5374 return static_cast<int32_t>(obj->Number());
5375 }
5376}
5377
5378
Steve Block6ded16b2010-05-10 14:33:55 +01005379uint32_t Uint32::Value() const {
Steve Block6ded16b2010-05-10 14:33:55 +01005380 i::Handle<i::Object> obj = Utils::OpenHandle(this);
5381 if (obj->IsSmi()) {
5382 return i::Smi::cast(*obj)->value();
5383 } else {
5384 return static_cast<uint32_t>(obj->Number());
5385 }
5386}
5387
5388
Steve Blocka7e24c12009-10-30 11:49:00 +00005389int v8::Object::InternalFieldCount() {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005390 i::Handle<i::JSReceiver> self = Utils::OpenHandle(this);
5391 if (!self->IsJSObject()) return 0;
5392 return i::Handle<i::JSObject>::cast(self)->GetInternalFieldCount();
Steve Blocka7e24c12009-10-30 11:49:00 +00005393}
5394
5395
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005396static bool InternalFieldOK(i::Handle<i::JSReceiver> obj, int index,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005397 const char* location) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005398 return Utils::ApiCheck(
5399 obj->IsJSObject() &&
5400 (index < i::Handle<i::JSObject>::cast(obj)->GetInternalFieldCount()),
5401 location, "Internal field out of bounds");
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005402}
5403
5404
5405Local<Value> v8::Object::SlowGetInternalField(int index) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005406 i::Handle<i::JSReceiver> obj = Utils::OpenHandle(this);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005407 const char* location = "v8::Object::GetInternalField()";
5408 if (!InternalFieldOK(obj, index, location)) return Local<Value>();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005409 i::Handle<i::Object> value(
5410 i::Handle<i::JSObject>::cast(obj)->GetInternalField(index),
5411 obj->GetIsolate());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005412 return Utils::ToLocal(value);
Steve Blocka7e24c12009-10-30 11:49:00 +00005413}
5414
5415
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005416void v8::Object::SetInternalField(int index, v8::Local<Value> value) {
5417 i::Handle<i::JSReceiver> obj = Utils::OpenHandle(this);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005418 const char* location = "v8::Object::SetInternalField()";
5419 if (!InternalFieldOK(obj, index, location)) return;
Steve Blocka7e24c12009-10-30 11:49:00 +00005420 i::Handle<i::Object> val = Utils::OpenHandle(*value);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005421 i::Handle<i::JSObject>::cast(obj)->SetInternalField(index, *val);
Steve Blocka7e24c12009-10-30 11:49:00 +00005422}
5423
5424
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005425void* v8::Object::SlowGetAlignedPointerFromInternalField(int index) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005426 i::Handle<i::JSReceiver> obj = Utils::OpenHandle(this);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005427 const char* location = "v8::Object::GetAlignedPointerFromInternalField()";
5428 if (!InternalFieldOK(obj, index, location)) return NULL;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005429 return DecodeSmiToAligned(
5430 i::Handle<i::JSObject>::cast(obj)->GetInternalField(index), location);
Ben Murdochb8e0da22011-05-16 14:20:40 +01005431}
5432
5433
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005434void v8::Object::SetAlignedPointerInInternalField(int index, void* value) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005435 i::Handle<i::JSReceiver> obj = Utils::OpenHandle(this);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005436 const char* location = "v8::Object::SetAlignedPointerInInternalField()";
5437 if (!InternalFieldOK(obj, index, location)) return;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005438 i::Handle<i::JSObject>::cast(obj)
5439 ->SetInternalField(index, EncodeAlignedAsSmi(value, location));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005440 DCHECK_EQ(value, GetAlignedPointerFromInternalField(index));
Ben Murdochb8e0da22011-05-16 14:20:40 +01005441}
5442
5443
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005444static void* ExternalValue(i::Object* obj) {
5445 // Obscure semantics for undefined, but somehow checked in our unit tests...
5446 if (obj->IsUndefined()) return NULL;
5447 i::Object* foreign = i::JSObject::cast(obj)->GetInternalField(0);
5448 return i::Foreign::cast(foreign)->foreign_address();
Steve Blocka7e24c12009-10-30 11:49:00 +00005449}
5450
5451
5452// --- E n v i r o n m e n t ---
5453
Steve Block44f0eee2011-05-26 01:26:41 +01005454
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005455void v8::V8::InitializePlatform(Platform* platform) {
5456 i::V8::InitializePlatform(platform);
Steve Blocka7e24c12009-10-30 11:49:00 +00005457}
5458
5459
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005460void v8::V8::ShutdownPlatform() {
5461 i::V8::ShutdownPlatform();
5462}
5463
5464
5465bool v8::V8::Initialize() {
5466 i::V8::Initialize();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005467#ifdef V8_USE_EXTERNAL_STARTUP_DATA
5468 i::ReadNatives();
5469#endif
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005470 return true;
5471}
5472
5473
5474void v8::V8::SetEntropySource(EntropySource entropy_source) {
5475 base::RandomNumberGenerator::SetEntropySource(entropy_source);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00005476}
5477
5478
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005479void v8::V8::SetReturnAddressLocationResolver(
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005480 ReturnAddressLocationResolver return_address_resolver) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005481 i::StackFrame::SetReturnAddressLocationResolver(return_address_resolver);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005482}
5483
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005484
Steve Blocka7e24c12009-10-30 11:49:00 +00005485bool v8::V8::Dispose() {
5486 i::V8::TearDown();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005487#ifdef V8_USE_EXTERNAL_STARTUP_DATA
5488 i::DisposeNatives();
5489#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00005490 return true;
5491}
5492
Ben Murdochda12d292016-06-02 14:46:10 +01005493HeapStatistics::HeapStatistics()
5494 : total_heap_size_(0),
5495 total_heap_size_executable_(0),
5496 total_physical_size_(0),
5497 total_available_size_(0),
5498 used_heap_size_(0),
5499 heap_size_limit_(0),
5500 malloced_memory_(0),
5501 does_zap_garbage_(0) {}
Steve Block3ce2e202009-11-05 08:53:23 +00005502
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005503HeapSpaceStatistics::HeapSpaceStatistics(): space_name_(0),
5504 space_size_(0),
5505 space_used_size_(0),
5506 space_available_size_(0),
5507 physical_space_size_(0) { }
5508
5509
5510HeapObjectStatistics::HeapObjectStatistics()
5511 : object_type_(nullptr),
5512 object_sub_type_(nullptr),
5513 object_count_(0),
5514 object_size_(0) {}
5515
5516
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005517bool v8::V8::InitializeICU(const char* icu_data_file) {
5518 return i::InitializeICU(icu_data_file);
Steve Block6ded16b2010-05-10 14:33:55 +01005519}
5520
5521
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005522void v8::V8::InitializeExternalStartupData(const char* directory_path) {
5523 i::InitializeExternalStartupData(directory_path);
5524}
5525
5526
5527void v8::V8::InitializeExternalStartupData(const char* natives_blob,
5528 const char* snapshot_blob) {
5529 i::InitializeExternalStartupData(natives_blob, snapshot_blob);
5530}
5531
5532
Steve Blocka7e24c12009-10-30 11:49:00 +00005533const char* v8::V8::GetVersion() {
Steve Block44f0eee2011-05-26 01:26:41 +01005534 return i::Version::GetVersion();
Steve Blocka7e24c12009-10-30 11:49:00 +00005535}
5536
5537
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005538static i::Handle<i::Context> CreateEnvironment(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005539 i::Isolate* isolate, v8::ExtensionConfiguration* extensions,
5540 v8::Local<ObjectTemplate> global_template,
5541 v8::Local<Value> maybe_global_proxy) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005542 i::Handle<i::Context> env;
Steve Blocka7e24c12009-10-30 11:49:00 +00005543
5544 // Enter V8 via an ENTER_V8 scope.
Steve Blocka7e24c12009-10-30 11:49:00 +00005545 {
Steve Block44f0eee2011-05-26 01:26:41 +01005546 ENTER_V8(isolate);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005547 v8::Local<ObjectTemplate> proxy_template = global_template;
Steve Blocka7e24c12009-10-30 11:49:00 +00005548 i::Handle<i::FunctionTemplateInfo> proxy_constructor;
5549 i::Handle<i::FunctionTemplateInfo> global_constructor;
5550
5551 if (!global_template.IsEmpty()) {
5552 // Make sure that the global_template has a constructor.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005553 global_constructor = EnsureConstructor(isolate, *global_template);
Steve Blocka7e24c12009-10-30 11:49:00 +00005554
5555 // Create a fresh template for the global proxy object.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005556 proxy_template = ObjectTemplate::New(
5557 reinterpret_cast<v8::Isolate*>(isolate));
5558 proxy_constructor = EnsureConstructor(isolate, *proxy_template);
Steve Blocka7e24c12009-10-30 11:49:00 +00005559
5560 // Set the global template to be the prototype template of
5561 // global proxy template.
5562 proxy_constructor->set_prototype_template(
5563 *Utils::OpenHandle(*global_template));
5564
5565 // Migrate security handlers from global_template to
5566 // proxy_template. Temporarily removing access check
5567 // information from the global template.
5568 if (!global_constructor->access_check_info()->IsUndefined()) {
5569 proxy_constructor->set_access_check_info(
5570 global_constructor->access_check_info());
5571 proxy_constructor->set_needs_access_check(
5572 global_constructor->needs_access_check());
5573 global_constructor->set_needs_access_check(false);
Steve Block44f0eee2011-05-26 01:26:41 +01005574 global_constructor->set_access_check_info(
5575 isolate->heap()->undefined_value());
Steve Blocka7e24c12009-10-30 11:49:00 +00005576 }
5577 }
5578
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005579 i::Handle<i::Object> proxy = Utils::OpenHandle(*maybe_global_proxy, true);
5580 i::MaybeHandle<i::JSGlobalProxy> maybe_proxy;
5581 if (!proxy.is_null()) {
5582 maybe_proxy = i::Handle<i::JSGlobalProxy>::cast(proxy);
5583 }
Steve Blocka7e24c12009-10-30 11:49:00 +00005584 // Create the environment.
Steve Block44f0eee2011-05-26 01:26:41 +01005585 env = isolate->bootstrapper()->CreateEnvironment(
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005586 maybe_proxy, proxy_template, extensions);
Steve Blocka7e24c12009-10-30 11:49:00 +00005587
5588 // Restore the access check info on the global template.
5589 if (!global_template.IsEmpty()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005590 DCHECK(!global_constructor.is_null());
5591 DCHECK(!proxy_constructor.is_null());
Steve Blocka7e24c12009-10-30 11:49:00 +00005592 global_constructor->set_access_check_info(
5593 proxy_constructor->access_check_info());
5594 global_constructor->set_needs_access_check(
5595 proxy_constructor->needs_access_check());
5596 }
5597 }
5598 // Leave V8.
5599
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005600 return env;
5601}
5602
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005603Local<Context> v8::Context::New(v8::Isolate* external_isolate,
5604 v8::ExtensionConfiguration* extensions,
5605 v8::Local<ObjectTemplate> global_template,
5606 v8::Local<Value> global_object) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005607 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(external_isolate);
Ben Murdochc5610432016-08-08 18:44:38 +01005608 LOG_API(isolate, Context, New);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005609 i::HandleScope scope(isolate);
5610 ExtensionConfiguration no_extensions;
5611 if (extensions == NULL) extensions = &no_extensions;
5612 i::Handle<i::Context> env =
5613 CreateEnvironment(isolate, extensions, global_template, global_object);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005614 if (env.is_null()) {
5615 if (isolate->has_pending_exception()) {
5616 isolate->OptionalRescheduleException(true);
5617 }
5618 return Local<Context>();
5619 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005620 return Utils::ToLocal(scope.CloseAndEscape(env));
Steve Blocka7e24c12009-10-30 11:49:00 +00005621}
5622
5623
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005624void v8::Context::SetSecurityToken(Local<Value> token) {
Steve Blocka7e24c12009-10-30 11:49:00 +00005625 i::Handle<i::Context> env = Utils::OpenHandle(this);
5626 i::Handle<i::Object> token_handle = Utils::OpenHandle(*token);
5627 env->set_security_token(*token_handle);
5628}
5629
5630
5631void v8::Context::UseDefaultSecurityToken() {
Steve Blocka7e24c12009-10-30 11:49:00 +00005632 i::Handle<i::Context> env = Utils::OpenHandle(this);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005633 env->set_security_token(env->global_object());
Steve Blocka7e24c12009-10-30 11:49:00 +00005634}
5635
5636
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005637Local<Value> v8::Context::GetSecurityToken() {
Steve Blocka7e24c12009-10-30 11:49:00 +00005638 i::Handle<i::Context> env = Utils::OpenHandle(this);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005639 i::Isolate* isolate = env->GetIsolate();
Steve Blocka7e24c12009-10-30 11:49:00 +00005640 i::Object* security_token = env->security_token();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005641 i::Handle<i::Object> token_handle(security_token, isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00005642 return Utils::ToLocal(token_handle);
5643}
5644
5645
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005646v8::Isolate* Context::GetIsolate() {
Steve Blocka7e24c12009-10-30 11:49:00 +00005647 i::Handle<i::Context> env = Utils::OpenHandle(this);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005648 return reinterpret_cast<Isolate*>(env->GetIsolate());
Steve Blocka7e24c12009-10-30 11:49:00 +00005649}
5650
5651
5652v8::Local<v8::Object> Context::Global() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005653 i::Handle<i::Context> context = Utils::OpenHandle(this);
5654 i::Isolate* isolate = context->GetIsolate();
5655 i::Handle<i::Object> global(context->global_proxy(), isolate);
5656 // TODO(dcarney): This should always return the global proxy
5657 // but can't presently as calls to GetProtoype will return the wrong result.
5658 if (i::Handle<i::JSGlobalProxy>::cast(
5659 global)->IsDetachedFrom(context->global_object())) {
5660 global = i::Handle<i::Object>(context->global_object(), isolate);
Steve Block44f0eee2011-05-26 01:26:41 +01005661 }
Steve Blocka7e24c12009-10-30 11:49:00 +00005662 return Utils::ToLocal(i::Handle<i::JSObject>::cast(global));
5663}
5664
5665
5666void Context::DetachGlobal() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005667 i::Handle<i::Context> context = Utils::OpenHandle(this);
5668 i::Isolate* isolate = context->GetIsolate();
Steve Block44f0eee2011-05-26 01:26:41 +01005669 ENTER_V8(isolate);
Steve Block44f0eee2011-05-26 01:26:41 +01005670 isolate->bootstrapper()->DetachGlobal(context);
Steve Blocka7e24c12009-10-30 11:49:00 +00005671}
5672
5673
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005674Local<v8::Object> Context::GetExtrasBindingObject() {
5675 i::Handle<i::Context> context = Utils::OpenHandle(this);
5676 i::Isolate* isolate = context->GetIsolate();
5677 i::Handle<i::JSObject> binding(context->extras_binding_object(), isolate);
5678 return Utils::ToLocal(binding);
5679}
5680
5681
Ben Murdoch257744e2011-11-30 15:57:28 +00005682void Context::AllowCodeGenerationFromStrings(bool allow) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005683 i::Handle<i::Context> context = Utils::OpenHandle(this);
5684 i::Isolate* isolate = context->GetIsolate();
Ben Murdoch257744e2011-11-30 15:57:28 +00005685 ENTER_V8(isolate);
Ben Murdoch257744e2011-11-30 15:57:28 +00005686 context->set_allow_code_gen_from_strings(
5687 allow ? isolate->heap()->true_value() : isolate->heap()->false_value());
5688}
5689
5690
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005691bool Context::IsCodeGenerationFromStringsAllowed() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005692 i::Handle<i::Context> context = Utils::OpenHandle(this);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005693 return !context->allow_code_gen_from_strings()->IsFalse();
5694}
5695
5696
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005697void Context::SetErrorMessageForCodeGenerationFromStrings(Local<String> error) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005698 i::Handle<i::Context> context = Utils::OpenHandle(this);
5699 i::Handle<i::String> error_handle = Utils::OpenHandle(*error);
5700 context->set_error_message_for_code_gen_from_strings(*error_handle);
Andrei Popescu74b3c142010-03-29 12:03:09 +01005701}
5702
5703
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005704size_t Context::EstimatedSize() {
5705 return static_cast<size_t>(
5706 i::ContextMeasure(*Utils::OpenHandle(this)).Size());
5707}
5708
5709
5710MaybeLocal<v8::Object> ObjectTemplate::NewInstance(Local<Context> context) {
Ben Murdochc5610432016-08-08 18:44:38 +01005711 PREPARE_FOR_EXECUTION(context, ObjectTemplate, NewInstance, Object);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005712 auto self = Utils::OpenHandle(this);
5713 Local<Object> result;
5714 has_pending_exception =
5715 !ToLocal<Object>(i::ApiNatives::InstantiateObject(self), &result);
5716 RETURN_ON_FAILED_EXECUTION(Object);
5717 RETURN_ESCAPED(result);
5718}
5719
5720
Steve Blocka7e24c12009-10-30 11:49:00 +00005721Local<v8::Object> ObjectTemplate::NewInstance() {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005722 auto context = ContextFromHeapObject(Utils::OpenHandle(this));
5723 RETURN_TO_LOCAL_UNCHECKED(NewInstance(context), Object);
5724}
5725
5726
5727MaybeLocal<v8::Function> FunctionTemplate::GetFunction(Local<Context> context) {
Ben Murdochc5610432016-08-08 18:44:38 +01005728 PREPARE_FOR_EXECUTION(context, FunctionTemplate, GetFunction, Function);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005729 auto self = Utils::OpenHandle(this);
5730 Local<Function> result;
5731 has_pending_exception =
5732 !ToLocal<Function>(i::ApiNatives::InstantiateFunction(self), &result);
5733 RETURN_ON_FAILED_EXECUTION(Function);
5734 RETURN_ESCAPED(result);
Steve Blocka7e24c12009-10-30 11:49:00 +00005735}
5736
5737
5738Local<v8::Function> FunctionTemplate::GetFunction() {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005739 auto context = ContextFromHeapObject(Utils::OpenHandle(this));
5740 RETURN_TO_LOCAL_UNCHECKED(GetFunction(context), Function);
Steve Blocka7e24c12009-10-30 11:49:00 +00005741}
5742
5743
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005744bool FunctionTemplate::HasInstance(v8::Local<v8::Value> value) {
5745 auto self = Utils::OpenHandle(this);
5746 auto obj = Utils::OpenHandle(*value);
5747 return self->IsTemplateFor(*obj);
Steve Blocka7e24c12009-10-30 11:49:00 +00005748}
5749
5750
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005751Local<External> v8::External::New(Isolate* isolate, void* value) {
5752 STATIC_ASSERT(sizeof(value) == sizeof(i::Address));
5753 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
Ben Murdochc5610432016-08-08 18:44:38 +01005754 LOG_API(i_isolate, External, New);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005755 ENTER_V8(i_isolate);
5756 i::Handle<i::JSObject> external = i_isolate->factory()->NewExternal(value);
5757 return Utils::ExternalToLocal(external);
Steve Blocka7e24c12009-10-30 11:49:00 +00005758}
5759
5760
5761void* External::Value() const {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005762 return ExternalValue(*Utils::OpenHandle(this));
Steve Blocka7e24c12009-10-30 11:49:00 +00005763}
5764
5765
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005766// anonymous namespace for string creation helper functions
5767namespace {
5768
5769inline int StringLength(const char* string) {
5770 return i::StrLength(string);
Steve Blocka7e24c12009-10-30 11:49:00 +00005771}
5772
5773
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005774inline int StringLength(const uint8_t* string) {
5775 return i::StrLength(reinterpret_cast<const char*>(string));
5776}
5777
5778
5779inline int StringLength(const uint16_t* string) {
5780 int length = 0;
5781 while (string[length] != '\0')
5782 length++;
5783 return length;
5784}
5785
5786
5787MUST_USE_RESULT
5788inline i::MaybeHandle<i::String> NewString(i::Factory* factory,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005789 v8::NewStringType type,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005790 i::Vector<const char> string) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005791 if (type == v8::NewStringType::kInternalized) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005792 return factory->InternalizeUtf8String(string);
5793 }
5794 return factory->NewStringFromUtf8(string);
5795}
5796
5797
5798MUST_USE_RESULT
5799inline i::MaybeHandle<i::String> NewString(i::Factory* factory,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005800 v8::NewStringType type,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005801 i::Vector<const uint8_t> string) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005802 if (type == v8::NewStringType::kInternalized) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005803 return factory->InternalizeOneByteString(string);
5804 }
5805 return factory->NewStringFromOneByte(string);
5806}
5807
5808
5809MUST_USE_RESULT
5810inline i::MaybeHandle<i::String> NewString(i::Factory* factory,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005811 v8::NewStringType type,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005812 i::Vector<const uint16_t> string) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005813 if (type == v8::NewStringType::kInternalized) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005814 return factory->InternalizeTwoByteString(string);
5815 }
5816 return factory->NewStringFromTwoByte(string);
5817}
5818
5819
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005820STATIC_ASSERT(v8::String::kMaxLength == i::String::kMaxLength);
5821
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005822} // anonymous namespace
5823
Ben Murdochc5610432016-08-08 18:44:38 +01005824// TODO(dcarney): throw a context free exception.
5825#define NEW_STRING(isolate, class_name, function_name, Char, data, type, \
5826 length) \
5827 MaybeLocal<String> result; \
5828 if (length == 0) { \
5829 result = String::Empty(isolate); \
5830 } else if (length > i::String::kMaxLength) { \
5831 result = MaybeLocal<String>(); \
5832 } else { \
5833 i::Isolate* i_isolate = reinterpret_cast<internal::Isolate*>(isolate); \
5834 ENTER_V8(i_isolate); \
5835 LOG_API(i_isolate, class_name, function_name); \
5836 if (length < 0) length = StringLength(data); \
5837 i::Handle<i::String> handle_result = \
5838 NewString(i_isolate->factory(), type, \
5839 i::Vector<const Char>(data, length)) \
5840 .ToHandleChecked(); \
5841 result = Utils::ToLocal(handle_result); \
5842 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005843
5844Local<String> String::NewFromUtf8(Isolate* isolate,
5845 const char* data,
5846 NewStringType type,
5847 int length) {
Ben Murdochc5610432016-08-08 18:44:38 +01005848 NEW_STRING(isolate, String, NewFromUtf8, char, data,
5849 static_cast<v8::NewStringType>(type), length);
5850 RETURN_TO_LOCAL_UNCHECKED(result, String);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005851}
5852
5853
5854MaybeLocal<String> String::NewFromUtf8(Isolate* isolate, const char* data,
5855 v8::NewStringType type, int length) {
Ben Murdochc5610432016-08-08 18:44:38 +01005856 NEW_STRING(isolate, String, NewFromUtf8, char, data, type, length);
5857 return result;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005858}
5859
5860
5861Local<String> String::NewFromOneByte(Isolate* isolate,
5862 const uint8_t* data,
5863 NewStringType type,
5864 int length) {
Ben Murdochc5610432016-08-08 18:44:38 +01005865 NEW_STRING(isolate, String, NewFromOneByte, uint8_t, data,
5866 static_cast<v8::NewStringType>(type), length);
5867 RETURN_TO_LOCAL_UNCHECKED(result, String);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005868}
5869
5870
5871MaybeLocal<String> String::NewFromOneByte(Isolate* isolate, const uint8_t* data,
5872 v8::NewStringType type, int length) {
Ben Murdochc5610432016-08-08 18:44:38 +01005873 NEW_STRING(isolate, String, NewFromOneByte, uint8_t, data, type, length);
5874 return result;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005875}
5876
5877
5878Local<String> String::NewFromTwoByte(Isolate* isolate,
5879 const uint16_t* data,
5880 NewStringType type,
5881 int length) {
Ben Murdochc5610432016-08-08 18:44:38 +01005882 NEW_STRING(isolate, String, NewFromTwoByte, uint16_t, data,
5883 static_cast<v8::NewStringType>(type), length);
5884 RETURN_TO_LOCAL_UNCHECKED(result, String);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005885}
5886
Steve Blocka7e24c12009-10-30 11:49:00 +00005887
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005888MaybeLocal<String> String::NewFromTwoByte(Isolate* isolate,
5889 const uint16_t* data,
5890 v8::NewStringType type, int length) {
Ben Murdochc5610432016-08-08 18:44:38 +01005891 NEW_STRING(isolate, String, NewFromTwoByte, uint16_t, data, type, length);
5892 return result;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005893}
5894
5895
5896Local<String> v8::String::Concat(Local<String> left, Local<String> right) {
Steve Block3ce2e202009-11-05 08:53:23 +00005897 i::Handle<i::String> left_string = Utils::OpenHandle(*left);
Steve Block44f0eee2011-05-26 01:26:41 +01005898 i::Isolate* isolate = left_string->GetIsolate();
Steve Block44f0eee2011-05-26 01:26:41 +01005899 ENTER_V8(isolate);
Ben Murdochc5610432016-08-08 18:44:38 +01005900 LOG_API(isolate, String, Concat);
Steve Block3ce2e202009-11-05 08:53:23 +00005901 i::Handle<i::String> right_string = Utils::OpenHandle(*right);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005902 // If we are steering towards a range error, do not wait for the error to be
5903 // thrown, and return the null handle instead.
5904 if (left_string->length() + right_string->length() > i::String::kMaxLength) {
5905 return Local<String>();
5906 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005907 i::Handle<i::String> result = isolate->factory()->NewConsString(
5908 left_string, right_string).ToHandleChecked();
Steve Block3ce2e202009-11-05 08:53:23 +00005909 return Utils::ToLocal(result);
5910}
5911
5912
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005913MaybeLocal<String> v8::String::NewExternalTwoByte(
5914 Isolate* isolate, v8::String::ExternalStringResource* resource) {
5915 CHECK(resource && resource->data());
5916 // TODO(dcarney): throw a context free exception.
5917 if (resource->length() > static_cast<size_t>(i::String::kMaxLength)) {
5918 return MaybeLocal<String>();
5919 }
5920 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
5921 ENTER_V8(i_isolate);
Ben Murdochc5610432016-08-08 18:44:38 +01005922 LOG_API(i_isolate, String, NewExternalTwoByte);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005923 i::Handle<i::String> string = i_isolate->factory()
5924 ->NewExternalStringFromTwoByte(resource)
5925 .ToHandleChecked();
5926 i_isolate->heap()->RegisterExternalString(*string);
5927 return Utils::ToLocal(string);
Steve Blocka7e24c12009-10-30 11:49:00 +00005928}
5929
5930
Steve Blocka7e24c12009-10-30 11:49:00 +00005931Local<String> v8::String::NewExternal(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005932 Isolate* isolate, v8::String::ExternalStringResource* resource) {
5933 RETURN_TO_LOCAL_UNCHECKED(NewExternalTwoByte(isolate, resource), String);
5934}
5935
5936
5937MaybeLocal<String> v8::String::NewExternalOneByte(
5938 Isolate* isolate, v8::String::ExternalOneByteStringResource* resource) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005939 CHECK(resource && resource->data());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005940 // TODO(dcarney): throw a context free exception.
5941 if (resource->length() > static_cast<size_t>(i::String::kMaxLength)) {
5942 return MaybeLocal<String>();
5943 }
5944 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
5945 ENTER_V8(i_isolate);
Ben Murdochc5610432016-08-08 18:44:38 +01005946 LOG_API(i_isolate, String, NewExternalOneByte);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005947 i::Handle<i::String> string = i_isolate->factory()
5948 ->NewExternalStringFromOneByte(resource)
5949 .ToHandleChecked();
5950 i_isolate->heap()->RegisterExternalString(*string);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005951 return Utils::ToLocal(string);
Steve Blocka7e24c12009-10-30 11:49:00 +00005952}
5953
5954
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005955Local<String> v8::String::NewExternal(
5956 Isolate* isolate, v8::String::ExternalOneByteStringResource* resource) {
5957 RETURN_TO_LOCAL_UNCHECKED(NewExternalOneByte(isolate, resource), String);
5958}
5959
5960
Steve Blocka7e24c12009-10-30 11:49:00 +00005961bool v8::String::MakeExternal(v8::String::ExternalStringResource* resource) {
Steve Blocka7e24c12009-10-30 11:49:00 +00005962 i::Handle<i::String> obj = Utils::OpenHandle(this);
Steve Block44f0eee2011-05-26 01:26:41 +01005963 i::Isolate* isolate = obj->GetIsolate();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005964 if (i::StringShape(*obj).IsExternal()) {
Steve Block44f0eee2011-05-26 01:26:41 +01005965 return false; // Already an external string.
5966 }
5967 ENTER_V8(isolate);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00005968 if (isolate->heap()->IsInGCPostProcessing()) {
5969 return false;
5970 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005971 CHECK(resource && resource->data());
5972
Steve Blocka7e24c12009-10-30 11:49:00 +00005973 bool result = obj->MakeExternal(resource);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005974 // Assert that if CanMakeExternal(), then externalizing actually succeeds.
5975 DCHECK(!CanMakeExternal() || result);
5976 if (result) {
5977 DCHECK(obj->IsExternalString());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005978 isolate->heap()->RegisterExternalString(*obj);
Steve Blocka7e24c12009-10-30 11:49:00 +00005979 }
5980 return result;
5981}
5982
5983
Steve Blocka7e24c12009-10-30 11:49:00 +00005984bool v8::String::MakeExternal(
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005985 v8::String::ExternalOneByteStringResource* resource) {
Steve Blocka7e24c12009-10-30 11:49:00 +00005986 i::Handle<i::String> obj = Utils::OpenHandle(this);
Steve Block44f0eee2011-05-26 01:26:41 +01005987 i::Isolate* isolate = obj->GetIsolate();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005988 if (i::StringShape(*obj).IsExternal()) {
Steve Block44f0eee2011-05-26 01:26:41 +01005989 return false; // Already an external string.
5990 }
5991 ENTER_V8(isolate);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00005992 if (isolate->heap()->IsInGCPostProcessing()) {
5993 return false;
5994 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005995 CHECK(resource && resource->data());
5996
Steve Blocka7e24c12009-10-30 11:49:00 +00005997 bool result = obj->MakeExternal(resource);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005998 // Assert that if CanMakeExternal(), then externalizing actually succeeds.
5999 DCHECK(!CanMakeExternal() || result);
6000 if (result) {
6001 DCHECK(obj->IsExternalString());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006002 isolate->heap()->RegisterExternalString(*obj);
Steve Blocka7e24c12009-10-30 11:49:00 +00006003 }
6004 return result;
6005}
6006
6007
6008bool v8::String::CanMakeExternal() {
Steve Blocka7e24c12009-10-30 11:49:00 +00006009 i::Handle<i::String> obj = Utils::OpenHandle(this);
Steve Block44f0eee2011-05-26 01:26:41 +01006010 i::Isolate* isolate = obj->GetIsolate();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006011
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006012 // Old space strings should be externalized.
6013 if (!isolate->heap()->new_space()->Contains(*obj)) return true;
Steve Blocka7e24c12009-10-30 11:49:00 +00006014 int size = obj->Size(); // Byte size of the original string.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006015 if (size <= i::ExternalString::kShortSize) return false;
Steve Blocka7e24c12009-10-30 11:49:00 +00006016 i::StringShape shape(*obj);
6017 return !shape.IsExternal();
6018}
6019
6020
Emily Bernierd0a1eb72015-03-24 16:35:39 -04006021Isolate* v8::Object::GetIsolate() {
6022 i::Isolate* i_isolate = Utils::OpenHandle(this)->GetIsolate();
6023 return reinterpret_cast<Isolate*>(i_isolate);
6024}
6025
6026
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006027Local<v8::Object> v8::Object::New(Isolate* isolate) {
6028 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
Ben Murdochc5610432016-08-08 18:44:38 +01006029 LOG_API(i_isolate, Object, New);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006030 ENTER_V8(i_isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00006031 i::Handle<i::JSObject> obj =
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006032 i_isolate->factory()->NewJSObject(i_isolate->object_function());
Steve Blocka7e24c12009-10-30 11:49:00 +00006033 return Utils::ToLocal(obj);
6034}
6035
6036
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006037Local<v8::Value> v8::NumberObject::New(Isolate* isolate, double value) {
6038 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
Ben Murdochc5610432016-08-08 18:44:38 +01006039 LOG_API(i_isolate, NumberObject, New);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006040 ENTER_V8(i_isolate);
6041 i::Handle<i::Object> number = i_isolate->factory()->NewNumber(value);
6042 i::Handle<i::Object> obj =
6043 i::Object::ToObject(i_isolate, number).ToHandleChecked();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00006044 return Utils::ToLocal(obj);
6045}
6046
6047
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006048double v8::NumberObject::ValueOf() const {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00006049 i::Handle<i::Object> obj = Utils::OpenHandle(this);
6050 i::Handle<i::JSValue> jsvalue = i::Handle<i::JSValue>::cast(obj);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006051 i::Isolate* isolate = jsvalue->GetIsolate();
Ben Murdochc5610432016-08-08 18:44:38 +01006052 LOG_API(isolate, NumberObject, NumberValue);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00006053 return jsvalue->value()->Number();
6054}
6055
6056
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006057Local<v8::Value> v8::BooleanObject::New(Isolate* isolate, bool value) {
6058 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
Ben Murdochc5610432016-08-08 18:44:38 +01006059 LOG_API(i_isolate, BooleanObject, New);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006060 ENTER_V8(i_isolate);
6061 i::Handle<i::Object> boolean(value ? i_isolate->heap()->true_value()
6062 : i_isolate->heap()->false_value(),
6063 i_isolate);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006064 i::Handle<i::Object> obj =
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006065 i::Object::ToObject(i_isolate, boolean).ToHandleChecked();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00006066 return Utils::ToLocal(obj);
6067}
6068
6069
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006070Local<v8::Value> v8::BooleanObject::New(bool value) {
6071 return New(Isolate::GetCurrent(), value);
6072}
6073
6074
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006075bool v8::BooleanObject::ValueOf() const {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00006076 i::Handle<i::Object> obj = Utils::OpenHandle(this);
6077 i::Handle<i::JSValue> jsvalue = i::Handle<i::JSValue>::cast(obj);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006078 i::Isolate* isolate = jsvalue->GetIsolate();
Ben Murdochc5610432016-08-08 18:44:38 +01006079 LOG_API(isolate, BooleanObject, BooleanValue);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00006080 return jsvalue->value()->IsTrue();
6081}
6082
6083
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006084Local<v8::Value> v8::StringObject::New(Local<String> value) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006085 i::Handle<i::String> string = Utils::OpenHandle(*value);
6086 i::Isolate* isolate = string->GetIsolate();
Ben Murdochc5610432016-08-08 18:44:38 +01006087 LOG_API(isolate, StringObject, New);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00006088 ENTER_V8(isolate);
6089 i::Handle<i::Object> obj =
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006090 i::Object::ToObject(isolate, string).ToHandleChecked();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00006091 return Utils::ToLocal(obj);
6092}
6093
6094
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006095Local<v8::String> v8::StringObject::ValueOf() const {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00006096 i::Handle<i::Object> obj = Utils::OpenHandle(this);
6097 i::Handle<i::JSValue> jsvalue = i::Handle<i::JSValue>::cast(obj);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006098 i::Isolate* isolate = jsvalue->GetIsolate();
Ben Murdochc5610432016-08-08 18:44:38 +01006099 LOG_API(isolate, StringObject, StringValue);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00006100 return Utils::ToLocal(
6101 i::Handle<i::String>(i::String::cast(jsvalue->value())));
6102}
6103
6104
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006105Local<v8::Value> v8::SymbolObject::New(Isolate* isolate, Local<Symbol> value) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006106 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
Ben Murdochc5610432016-08-08 18:44:38 +01006107 LOG_API(i_isolate, SymbolObject, New);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006108 ENTER_V8(i_isolate);
6109 i::Handle<i::Object> obj = i::Object::ToObject(
6110 i_isolate, Utils::OpenHandle(*value)).ToHandleChecked();
Steve Blocka7e24c12009-10-30 11:49:00 +00006111 return Utils::ToLocal(obj);
6112}
6113
6114
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006115Local<v8::Symbol> v8::SymbolObject::ValueOf() const {
6116 i::Handle<i::Object> obj = Utils::OpenHandle(this);
6117 i::Handle<i::JSValue> jsvalue = i::Handle<i::JSValue>::cast(obj);
6118 i::Isolate* isolate = jsvalue->GetIsolate();
Ben Murdochc5610432016-08-08 18:44:38 +01006119 LOG_API(isolate, SymbolObject, SymbolValue);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006120 return Utils::ToLocal(
6121 i::Handle<i::Symbol>(i::Symbol::cast(jsvalue->value())));
6122}
6123
6124
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006125MaybeLocal<v8::Value> v8::Date::New(Local<Context> context, double time) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006126 if (std::isnan(time)) {
6127 // Introduce only canonical NaN value into the VM, to avoid signaling NaNs.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006128 time = std::numeric_limits<double>::quiet_NaN();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006129 }
Ben Murdochc5610432016-08-08 18:44:38 +01006130 PREPARE_FOR_EXECUTION(context, Date, New, Value);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006131 Local<Value> result;
6132 has_pending_exception = !ToLocal<Value>(
6133 i::JSDate::New(isolate->date_function(), isolate->date_function(), time),
6134 &result);
6135 RETURN_ON_FAILED_EXECUTION(Value);
6136 RETURN_ESCAPED(result);
6137}
6138
6139
6140Local<v8::Value> v8::Date::New(Isolate* isolate, double time) {
6141 auto context = isolate->GetCurrentContext();
6142 RETURN_TO_LOCAL_UNCHECKED(New(context, time), Value);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006143}
6144
6145
6146double v8::Date::ValueOf() const {
Steve Blocka7e24c12009-10-30 11:49:00 +00006147 i::Handle<i::Object> obj = Utils::OpenHandle(this);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01006148 i::Handle<i::JSDate> jsdate = i::Handle<i::JSDate>::cast(obj);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006149 i::Isolate* isolate = jsdate->GetIsolate();
Ben Murdochc5610432016-08-08 18:44:38 +01006150 LOG_API(isolate, Date, NumberValue);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01006151 return jsdate->value()->Number();
Steve Blocka7e24c12009-10-30 11:49:00 +00006152}
6153
6154
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006155void v8::Date::DateTimeConfigurationChangeNotification(Isolate* isolate) {
6156 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
Ben Murdochc5610432016-08-08 18:44:38 +01006157 LOG_API(i_isolate, Date, DateTimeConfigurationChangeNotification);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006158 ENTER_V8(i_isolate);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006159 i_isolate->date_cache()->ResetDateCache();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006160 if (!i_isolate->eternal_handles()->Exists(
6161 i::EternalHandles::DATE_CACHE_VERSION)) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01006162 return;
6163 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006164 i::Handle<i::FixedArray> date_cache_version =
6165 i::Handle<i::FixedArray>::cast(i_isolate->eternal_handles()->GetSingleton(
6166 i::EternalHandles::DATE_CACHE_VERSION));
6167 DCHECK_EQ(1, date_cache_version->length());
6168 CHECK(date_cache_version->get(0)->IsSmi());
6169 date_cache_version->set(
6170 0,
6171 i::Smi::FromInt(i::Smi::cast(date_cache_version->get(0))->value() + 1));
Ben Murdochb0fe1622011-05-05 13:52:32 +01006172}
6173
6174
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006175MaybeLocal<v8::RegExp> v8::RegExp::New(Local<Context> context,
6176 Local<String> pattern, Flags flags) {
Ben Murdochc5610432016-08-08 18:44:38 +01006177 PREPARE_FOR_EXECUTION(context, RegExp, New, RegExp);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006178 Local<v8::RegExp> result;
6179 has_pending_exception =
6180 !ToLocal<RegExp>(i::JSRegExp::New(Utils::OpenHandle(*pattern),
6181 static_cast<i::JSRegExp::Flags>(flags)),
6182 &result);
6183 RETURN_ON_FAILED_EXECUTION(RegExp);
6184 RETURN_ESCAPED(result);
Ben Murdochf87a2032010-10-22 12:50:53 +01006185}
6186
6187
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006188Local<v8::RegExp> v8::RegExp::New(Local<String> pattern, Flags flags) {
6189 auto isolate =
6190 reinterpret_cast<Isolate*>(Utils::OpenHandle(*pattern)->GetIsolate());
6191 auto context = isolate->GetCurrentContext();
6192 RETURN_TO_LOCAL_UNCHECKED(New(context, pattern, flags), RegExp);
Ben Murdochf87a2032010-10-22 12:50:53 +01006193}
6194
6195
6196Local<v8::String> v8::RegExp::GetSource() const {
Ben Murdochf87a2032010-10-22 12:50:53 +01006197 i::Handle<i::JSRegExp> obj = Utils::OpenHandle(this);
6198 return Utils::ToLocal(i::Handle<i::String>(obj->Pattern()));
6199}
6200
6201
6202// Assert that the static flags cast in GetFlags is valid.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006203#define REGEXP_FLAG_ASSERT_EQ(flag) \
6204 STATIC_ASSERT(static_cast<int>(v8::RegExp::flag) == \
6205 static_cast<int>(i::JSRegExp::flag))
6206REGEXP_FLAG_ASSERT_EQ(kNone);
6207REGEXP_FLAG_ASSERT_EQ(kGlobal);
6208REGEXP_FLAG_ASSERT_EQ(kIgnoreCase);
6209REGEXP_FLAG_ASSERT_EQ(kMultiline);
6210REGEXP_FLAG_ASSERT_EQ(kSticky);
6211REGEXP_FLAG_ASSERT_EQ(kUnicode);
Ben Murdochf87a2032010-10-22 12:50:53 +01006212#undef REGEXP_FLAG_ASSERT_EQ
6213
6214v8::RegExp::Flags v8::RegExp::GetFlags() const {
Ben Murdochf87a2032010-10-22 12:50:53 +01006215 i::Handle<i::JSRegExp> obj = Utils::OpenHandle(this);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006216 return RegExp::Flags(static_cast<int>(obj->GetFlags()));
Ben Murdochf87a2032010-10-22 12:50:53 +01006217}
6218
6219
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006220Local<v8::Array> v8::Array::New(Isolate* isolate, int length) {
6221 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
Ben Murdochc5610432016-08-08 18:44:38 +01006222 LOG_API(i_isolate, Array, New);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006223 ENTER_V8(i_isolate);
Steve Block44f0eee2011-05-26 01:26:41 +01006224 int real_length = length > 0 ? length : 0;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006225 i::Handle<i::JSArray> obj = i_isolate->factory()->NewJSArray(real_length);
Ben Murdoch8b112d22011-06-08 16:22:53 +01006226 i::Handle<i::Object> length_obj =
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006227 i_isolate->factory()->NewNumberFromInt(real_length);
Ben Murdoch8b112d22011-06-08 16:22:53 +01006228 obj->set_length(*length_obj);
Steve Blocka7e24c12009-10-30 11:49:00 +00006229 return Utils::ToLocal(obj);
6230}
6231
6232
6233uint32_t v8::Array::Length() const {
Steve Blocka7e24c12009-10-30 11:49:00 +00006234 i::Handle<i::JSArray> obj = Utils::OpenHandle(this);
6235 i::Object* length = obj->length();
6236 if (length->IsSmi()) {
6237 return i::Smi::cast(length)->value();
6238 } else {
6239 return static_cast<uint32_t>(length->Number());
6240 }
6241}
6242
6243
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006244MaybeLocal<Object> Array::CloneElementAt(Local<Context> context,
6245 uint32_t index) {
Ben Murdochc5610432016-08-08 18:44:38 +01006246 PREPARE_FOR_EXECUTION(context, Array, CloneElementAt, Object);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006247 auto self = Utils::OpenHandle(this);
6248 if (!self->HasFastObjectElements()) return Local<Object>();
Steve Blocka7e24c12009-10-30 11:49:00 +00006249 i::FixedArray* elms = i::FixedArray::cast(self->elements());
6250 i::Object* paragon = elms->get(index);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006251 if (!paragon->IsJSObject()) return Local<Object>();
Steve Blocka7e24c12009-10-30 11:49:00 +00006252 i::Handle<i::JSObject> paragon_handle(i::JSObject::cast(paragon));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006253 Local<Object> result;
6254 has_pending_exception =
6255 !ToLocal<Object>(isolate->factory()->CopyJSObject(paragon_handle),
6256 &result);
6257 RETURN_ON_FAILED_EXECUTION(Object);
6258 RETURN_ESCAPED(result);
Steve Blocka7e24c12009-10-30 11:49:00 +00006259}
6260
6261
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006262Local<Object> Array::CloneElementAt(uint32_t index) { return Local<Object>(); }
6263
6264
6265Local<v8::Map> v8::Map::New(Isolate* isolate) {
6266 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
Ben Murdochc5610432016-08-08 18:44:38 +01006267 LOG_API(i_isolate, Map, New);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006268 ENTER_V8(i_isolate);
6269 i::Handle<i::JSMap> obj = i_isolate->factory()->NewJSMap();
6270 return Utils::ToLocal(obj);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006271}
6272
6273
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006274size_t v8::Map::Size() const {
6275 i::Handle<i::JSMap> obj = Utils::OpenHandle(this);
6276 return i::OrderedHashMap::cast(obj->table())->NumberOfElements();
6277}
6278
6279
6280void Map::Clear() {
6281 auto self = Utils::OpenHandle(this);
6282 i::Isolate* isolate = self->GetIsolate();
Ben Murdochc5610432016-08-08 18:44:38 +01006283 LOG_API(isolate, Map, Clear);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006284 ENTER_V8(isolate);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006285 i::JSMap::Clear(self);
6286}
6287
6288
6289MaybeLocal<Value> Map::Get(Local<Context> context, Local<Value> key) {
Ben Murdochc5610432016-08-08 18:44:38 +01006290 PREPARE_FOR_EXECUTION(context, Map, Get, Value);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006291 auto self = Utils::OpenHandle(this);
6292 Local<Value> result;
6293 i::Handle<i::Object> argv[] = {Utils::OpenHandle(*key)};
6294 has_pending_exception =
6295 !ToLocal<Value>(i::Execution::Call(isolate, isolate->map_get(), self,
6296 arraysize(argv), argv),
6297 &result);
6298 RETURN_ON_FAILED_EXECUTION(Value);
6299 RETURN_ESCAPED(result);
6300}
6301
6302
6303MaybeLocal<Map> Map::Set(Local<Context> context, Local<Value> key,
6304 Local<Value> value) {
Ben Murdochc5610432016-08-08 18:44:38 +01006305 PREPARE_FOR_EXECUTION(context, Map, Set, Map);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006306 auto self = Utils::OpenHandle(this);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006307 i::Handle<i::Object> result;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006308 i::Handle<i::Object> argv[] = {Utils::OpenHandle(*key),
6309 Utils::OpenHandle(*value)};
6310 has_pending_exception = !i::Execution::Call(isolate, isolate->map_set(), self,
6311 arraysize(argv), argv)
6312 .ToHandle(&result);
6313 RETURN_ON_FAILED_EXECUTION(Map);
6314 RETURN_ESCAPED(Local<Map>::Cast(Utils::ToLocal(result)));
6315}
6316
6317
6318Maybe<bool> Map::Has(Local<Context> context, Local<Value> key) {
Ben Murdochc5610432016-08-08 18:44:38 +01006319 PREPARE_FOR_EXECUTION_PRIMITIVE(context, Map, Has, bool);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006320 auto self = Utils::OpenHandle(this);
6321 i::Handle<i::Object> result;
6322 i::Handle<i::Object> argv[] = {Utils::OpenHandle(*key)};
6323 has_pending_exception = !i::Execution::Call(isolate, isolate->map_has(), self,
6324 arraysize(argv), argv)
6325 .ToHandle(&result);
6326 RETURN_ON_FAILED_EXECUTION_PRIMITIVE(bool);
6327 return Just(result->IsTrue());
6328}
6329
6330
6331Maybe<bool> Map::Delete(Local<Context> context, Local<Value> key) {
Ben Murdochc5610432016-08-08 18:44:38 +01006332 PREPARE_FOR_EXECUTION_PRIMITIVE(context, Map, Delete, bool);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006333 auto self = Utils::OpenHandle(this);
6334 i::Handle<i::Object> result;
6335 i::Handle<i::Object> argv[] = {Utils::OpenHandle(*key)};
6336 has_pending_exception = !i::Execution::Call(isolate, isolate->map_delete(),
6337 self, arraysize(argv), argv)
6338 .ToHandle(&result);
6339 RETURN_ON_FAILED_EXECUTION_PRIMITIVE(bool);
6340 return Just(result->IsTrue());
6341}
6342
6343
6344Local<Array> Map::AsArray() const {
6345 i::Handle<i::JSMap> obj = Utils::OpenHandle(this);
6346 i::Isolate* isolate = obj->GetIsolate();
6347 i::Factory* factory = isolate->factory();
Ben Murdochc5610432016-08-08 18:44:38 +01006348 LOG_API(isolate, Map, AsArray);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006349 ENTER_V8(isolate);
6350 i::Handle<i::OrderedHashMap> table(i::OrderedHashMap::cast(obj->table()));
Ben Murdochc5610432016-08-08 18:44:38 +01006351 int length = table->NumberOfElements() * 2;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006352 i::Handle<i::FixedArray> result = factory->NewFixedArray(length);
Ben Murdochc5610432016-08-08 18:44:38 +01006353 int result_index = 0;
6354 {
6355 i::DisallowHeapAllocation no_gc;
6356 int capacity = table->UsedCapacity();
6357 i::Oddball* the_hole = isolate->heap()->the_hole_value();
6358 for (int i = 0; i < capacity; ++i) {
6359 i::Object* key = table->KeyAt(i);
6360 if (key == the_hole) continue;
6361 result->set(result_index++, key);
6362 result->set(result_index++, table->ValueAt(i));
6363 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006364 }
Ben Murdochc5610432016-08-08 18:44:38 +01006365 DCHECK_EQ(result_index, result->length());
6366 DCHECK_EQ(result_index, length);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006367 i::Handle<i::JSArray> result_array =
6368 factory->NewJSArrayWithElements(result, i::FAST_ELEMENTS, length);
6369 return Utils::ToLocal(result_array);
6370}
6371
6372
6373Local<v8::Set> v8::Set::New(Isolate* isolate) {
6374 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
Ben Murdochc5610432016-08-08 18:44:38 +01006375 LOG_API(i_isolate, Set, New);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006376 ENTER_V8(i_isolate);
6377 i::Handle<i::JSSet> obj = i_isolate->factory()->NewJSSet();
6378 return Utils::ToLocal(obj);
6379}
6380
6381
6382size_t v8::Set::Size() const {
6383 i::Handle<i::JSSet> obj = Utils::OpenHandle(this);
6384 return i::OrderedHashSet::cast(obj->table())->NumberOfElements();
6385}
6386
6387
6388void Set::Clear() {
6389 auto self = Utils::OpenHandle(this);
6390 i::Isolate* isolate = self->GetIsolate();
Ben Murdochc5610432016-08-08 18:44:38 +01006391 LOG_API(isolate, Set, Clear);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006392 ENTER_V8(isolate);
6393 i::JSSet::Clear(self);
6394}
6395
6396
6397MaybeLocal<Set> Set::Add(Local<Context> context, Local<Value> key) {
Ben Murdochc5610432016-08-08 18:44:38 +01006398 PREPARE_FOR_EXECUTION(context, Set, Add, Set);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006399 auto self = Utils::OpenHandle(this);
6400 i::Handle<i::Object> result;
6401 i::Handle<i::Object> argv[] = {Utils::OpenHandle(*key)};
6402 has_pending_exception = !i::Execution::Call(isolate, isolate->set_add(), self,
6403 arraysize(argv), argv)
6404 .ToHandle(&result);
6405 RETURN_ON_FAILED_EXECUTION(Set);
6406 RETURN_ESCAPED(Local<Set>::Cast(Utils::ToLocal(result)));
6407}
6408
6409
6410Maybe<bool> Set::Has(Local<Context> context, Local<Value> key) {
Ben Murdochc5610432016-08-08 18:44:38 +01006411 PREPARE_FOR_EXECUTION_PRIMITIVE(context, Set, Has, bool);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006412 auto self = Utils::OpenHandle(this);
6413 i::Handle<i::Object> result;
6414 i::Handle<i::Object> argv[] = {Utils::OpenHandle(*key)};
6415 has_pending_exception = !i::Execution::Call(isolate, isolate->set_has(), self,
6416 arraysize(argv), argv)
6417 .ToHandle(&result);
6418 RETURN_ON_FAILED_EXECUTION_PRIMITIVE(bool);
6419 return Just(result->IsTrue());
6420}
6421
6422
6423Maybe<bool> Set::Delete(Local<Context> context, Local<Value> key) {
Ben Murdochc5610432016-08-08 18:44:38 +01006424 PREPARE_FOR_EXECUTION_PRIMITIVE(context, Set, Delete, bool);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006425 auto self = Utils::OpenHandle(this);
6426 i::Handle<i::Object> result;
6427 i::Handle<i::Object> argv[] = {Utils::OpenHandle(*key)};
6428 has_pending_exception = !i::Execution::Call(isolate, isolate->set_delete(),
6429 self, arraysize(argv), argv)
6430 .ToHandle(&result);
6431 RETURN_ON_FAILED_EXECUTION_PRIMITIVE(bool);
6432 return Just(result->IsTrue());
6433}
6434
6435
6436Local<Array> Set::AsArray() const {
6437 i::Handle<i::JSSet> obj = Utils::OpenHandle(this);
6438 i::Isolate* isolate = obj->GetIsolate();
6439 i::Factory* factory = isolate->factory();
Ben Murdochc5610432016-08-08 18:44:38 +01006440 LOG_API(isolate, Set, AsArray);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006441 ENTER_V8(isolate);
6442 i::Handle<i::OrderedHashSet> table(i::OrderedHashSet::cast(obj->table()));
6443 int length = table->NumberOfElements();
6444 i::Handle<i::FixedArray> result = factory->NewFixedArray(length);
Ben Murdochc5610432016-08-08 18:44:38 +01006445 int result_index = 0;
6446 {
6447 i::DisallowHeapAllocation no_gc;
6448 int capacity = table->UsedCapacity();
6449 i::Oddball* the_hole = isolate->heap()->the_hole_value();
6450 for (int i = 0; i < capacity; ++i) {
6451 i::Object* key = table->KeyAt(i);
6452 if (key == the_hole) continue;
6453 result->set(result_index++, key);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006454 }
6455 }
Ben Murdochc5610432016-08-08 18:44:38 +01006456 DCHECK_EQ(result_index, result->length());
6457 DCHECK_EQ(result_index, length);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006458 i::Handle<i::JSArray> result_array =
6459 factory->NewJSArrayWithElements(result, i::FAST_ELEMENTS, length);
6460 return Utils::ToLocal(result_array);
6461}
6462
6463
6464MaybeLocal<Promise::Resolver> Promise::Resolver::New(Local<Context> context) {
Ben Murdochc5610432016-08-08 18:44:38 +01006465 PREPARE_FOR_EXECUTION(context, Promise_Resolver, New, Resolver);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006466 i::Handle<i::Object> result;
6467 has_pending_exception =
6468 !i::Execution::Call(isolate, isolate->promise_create(),
6469 isolate->factory()->undefined_value(), 0, NULL)
6470 .ToHandle(&result);
6471 RETURN_ON_FAILED_EXECUTION(Promise::Resolver);
6472 RETURN_ESCAPED(Local<Promise::Resolver>::Cast(Utils::ToLocal(result)));
6473}
6474
6475
6476Local<Promise::Resolver> Promise::Resolver::New(Isolate* isolate) {
6477 RETURN_TO_LOCAL_UNCHECKED(New(isolate->GetCurrentContext()),
6478 Promise::Resolver);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006479}
6480
6481
6482Local<Promise> Promise::Resolver::GetPromise() {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006483 i::Handle<i::JSReceiver> promise = Utils::OpenHandle(this);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006484 return Local<Promise>::Cast(Utils::ToLocal(promise));
6485}
6486
6487
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006488Maybe<bool> Promise::Resolver::Resolve(Local<Context> context,
6489 Local<Value> value) {
Ben Murdochc5610432016-08-08 18:44:38 +01006490 PREPARE_FOR_EXECUTION_PRIMITIVE(context, Promise_Resolver, Resolve, bool);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006491 auto self = Utils::OpenHandle(this);
6492 i::Handle<i::Object> argv[] = {self, Utils::OpenHandle(*value)};
6493 has_pending_exception =
6494 i::Execution::Call(isolate, isolate->promise_resolve(),
6495 isolate->factory()->undefined_value(), arraysize(argv),
6496 argv)
6497 .is_null();
6498 RETURN_ON_FAILED_EXECUTION_PRIMITIVE(bool);
6499 return Just(true);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006500}
6501
6502
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006503void Promise::Resolver::Resolve(Local<Value> value) {
6504 auto context = ContextFromHeapObject(Utils::OpenHandle(this));
6505 USE(Resolve(context, value));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006506}
6507
6508
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006509Maybe<bool> Promise::Resolver::Reject(Local<Context> context,
6510 Local<Value> value) {
Ben Murdochc5610432016-08-08 18:44:38 +01006511 PREPARE_FOR_EXECUTION_PRIMITIVE(context, Promise_Resolver, Resolve, bool);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006512 auto self = Utils::OpenHandle(this);
6513 i::Handle<i::Object> argv[] = {self, Utils::OpenHandle(*value)};
6514 has_pending_exception =
6515 i::Execution::Call(isolate, isolate->promise_reject(),
6516 isolate->factory()->undefined_value(), arraysize(argv),
6517 argv)
6518 .is_null();
6519 RETURN_ON_FAILED_EXECUTION_PRIMITIVE(bool);
6520 return Just(true);
6521}
6522
6523
6524void Promise::Resolver::Reject(Local<Value> value) {
6525 auto context = ContextFromHeapObject(Utils::OpenHandle(this));
6526 USE(Reject(context, value));
6527}
6528
6529
6530namespace {
6531
6532MaybeLocal<Promise> DoChain(Value* value, Local<Context> context,
6533 Local<Function> handler) {
Ben Murdochc5610432016-08-08 18:44:38 +01006534 PREPARE_FOR_EXECUTION(context, Promise, Chain, Promise);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006535 auto self = Utils::OpenHandle(value);
6536 i::Handle<i::Object> argv[] = {Utils::OpenHandle(*handler)};
6537 i::Handle<i::Object> result;
6538 has_pending_exception = !i::Execution::Call(isolate, isolate->promise_chain(),
6539 self, arraysize(argv), argv)
6540 .ToHandle(&result);
6541 RETURN_ON_FAILED_EXECUTION(Promise);
6542 RETURN_ESCAPED(Local<Promise>::Cast(Utils::ToLocal(result)));
6543}
6544
6545} // namespace
6546
6547
6548MaybeLocal<Promise> Promise::Chain(Local<Context> context,
6549 Local<Function> handler) {
6550 return DoChain(this, context, handler);
6551}
6552
6553
6554Local<Promise> Promise::Chain(Local<Function> handler) {
6555 auto context = ContextFromHeapObject(Utils::OpenHandle(this));
6556 RETURN_TO_LOCAL_UNCHECKED(DoChain(this, context, handler), Promise);
6557}
6558
6559
6560MaybeLocal<Promise> Promise::Catch(Local<Context> context,
6561 Local<Function> handler) {
Ben Murdochc5610432016-08-08 18:44:38 +01006562 PREPARE_FOR_EXECUTION(context, Promise, Catch, Promise);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006563 auto self = Utils::OpenHandle(this);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006564 i::Handle<i::Object> argv[] = { Utils::OpenHandle(*handler) };
6565 i::Handle<i::Object> result;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006566 has_pending_exception = !i::Execution::Call(isolate, isolate->promise_catch(),
6567 self, arraysize(argv), argv)
6568 .ToHandle(&result);
6569 RETURN_ON_FAILED_EXECUTION(Promise);
6570 RETURN_ESCAPED(Local<Promise>::Cast(Utils::ToLocal(result)));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006571}
6572
6573
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006574Local<Promise> Promise::Catch(Local<Function> handler) {
6575 auto context = ContextFromHeapObject(Utils::OpenHandle(this));
6576 RETURN_TO_LOCAL_UNCHECKED(Catch(context, handler), Promise);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006577}
6578
6579
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006580MaybeLocal<Promise> Promise::Then(Local<Context> context,
6581 Local<Function> handler) {
Ben Murdochc5610432016-08-08 18:44:38 +01006582 PREPARE_FOR_EXECUTION(context, Promise, Then, Promise);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006583 auto self = Utils::OpenHandle(this);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006584 i::Handle<i::Object> argv[] = { Utils::OpenHandle(*handler) };
6585 i::Handle<i::Object> result;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006586 has_pending_exception = !i::Execution::Call(isolate, isolate->promise_then(),
6587 self, arraysize(argv), argv)
6588 .ToHandle(&result);
6589 RETURN_ON_FAILED_EXECUTION(Promise);
6590 RETURN_ESCAPED(Local<Promise>::Cast(Utils::ToLocal(result)));
6591}
6592
6593
6594Local<Promise> Promise::Then(Local<Function> handler) {
6595 auto context = ContextFromHeapObject(Utils::OpenHandle(this));
6596 RETURN_TO_LOCAL_UNCHECKED(Then(context, handler), Promise);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006597}
6598
6599
Emily Bernierd0a1eb72015-03-24 16:35:39 -04006600bool Promise::HasHandler() {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006601 i::Handle<i::JSReceiver> promise = Utils::OpenHandle(this);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04006602 i::Isolate* isolate = promise->GetIsolate();
Ben Murdochc5610432016-08-08 18:44:38 +01006603 LOG_API(isolate, Promise, HasRejectHandler);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04006604 ENTER_V8(isolate);
6605 i::Handle<i::Symbol> key = isolate->factory()->promise_has_handler_symbol();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006606 return i::JSReceiver::GetDataProperty(promise, key)->IsTrue();
Emily Bernierd0a1eb72015-03-24 16:35:39 -04006607}
6608
6609
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006610Local<Object> Proxy::GetTarget() {
6611 i::Handle<i::JSProxy> self = Utils::OpenHandle(this);
6612 i::Handle<i::JSReceiver> target(self->target());
6613 return Utils::ToLocal(target);
6614}
6615
6616
6617Local<Value> Proxy::GetHandler() {
6618 i::Handle<i::JSProxy> self = Utils::OpenHandle(this);
6619 i::Handle<i::Object> handler(self->handler(), self->GetIsolate());
6620 return Utils::ToLocal(handler);
6621}
6622
6623
6624bool Proxy::IsRevoked() {
6625 i::Handle<i::JSProxy> self = Utils::OpenHandle(this);
6626 return self->IsRevoked();
6627}
6628
6629
6630void Proxy::Revoke() {
6631 i::Handle<i::JSProxy> self = Utils::OpenHandle(this);
6632 i::JSProxy::Revoke(self);
6633}
6634
6635
6636MaybeLocal<Proxy> Proxy::New(Local<Context> context, Local<Object> local_target,
6637 Local<Object> local_handler) {
Ben Murdochc5610432016-08-08 18:44:38 +01006638 PREPARE_FOR_EXECUTION(context, Proxy, New, Proxy);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006639 i::Handle<i::JSReceiver> target = Utils::OpenHandle(*local_target);
6640 i::Handle<i::JSReceiver> handler = Utils::OpenHandle(*local_handler);
6641 Local<Proxy> result;
6642 has_pending_exception =
6643 !ToLocal<Proxy>(i::JSProxy::New(isolate, target, handler), &result);
6644 RETURN_ON_FAILED_EXECUTION(Proxy);
6645 RETURN_ESCAPED(result);
6646}
6647
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006648bool v8::ArrayBuffer::IsExternal() const {
6649 return Utils::OpenHandle(this)->is_external();
6650}
6651
6652
Emily Bernierd0a1eb72015-03-24 16:35:39 -04006653bool v8::ArrayBuffer::IsNeuterable() const {
6654 return Utils::OpenHandle(this)->is_neuterable();
6655}
6656
6657
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006658v8::ArrayBuffer::Contents v8::ArrayBuffer::Externalize() {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006659 i::Handle<i::JSArrayBuffer> self = Utils::OpenHandle(this);
6660 i::Isolate* isolate = self->GetIsolate();
Ben Murdochc5610432016-08-08 18:44:38 +01006661 Utils::ApiCheck(!self->is_external(), "v8_ArrayBuffer_Externalize",
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006662 "ArrayBuffer already externalized");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006663 self->set_is_external(true);
6664 isolate->heap()->UnregisterArrayBuffer(*self);
6665
6666 return GetContents();
6667}
6668
6669
6670v8::ArrayBuffer::Contents v8::ArrayBuffer::GetContents() {
6671 i::Handle<i::JSArrayBuffer> self = Utils::OpenHandle(this);
6672 size_t byte_length = static_cast<size_t>(self->byte_length()->Number());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006673 Contents contents;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006674 contents.data_ = self->backing_store();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006675 contents.byte_length_ = byte_length;
6676 return contents;
6677}
6678
6679
6680void v8::ArrayBuffer::Neuter() {
6681 i::Handle<i::JSArrayBuffer> obj = Utils::OpenHandle(this);
6682 i::Isolate* isolate = obj->GetIsolate();
6683 Utils::ApiCheck(obj->is_external(),
6684 "v8::ArrayBuffer::Neuter",
6685 "Only externalized ArrayBuffers can be neutered");
Emily Bernierd0a1eb72015-03-24 16:35:39 -04006686 Utils::ApiCheck(obj->is_neuterable(), "v8::ArrayBuffer::Neuter",
6687 "Only neuterable ArrayBuffers can be neutered");
Ben Murdochc5610432016-08-08 18:44:38 +01006688 LOG_API(obj->GetIsolate(), ArrayBuffer, Neuter);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006689 ENTER_V8(isolate);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006690 obj->Neuter();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006691}
6692
6693
6694size_t v8::ArrayBuffer::ByteLength() const {
6695 i::Handle<i::JSArrayBuffer> obj = Utils::OpenHandle(this);
6696 return static_cast<size_t>(obj->byte_length()->Number());
6697}
6698
6699
6700Local<ArrayBuffer> v8::ArrayBuffer::New(Isolate* isolate, size_t byte_length) {
6701 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
Ben Murdochc5610432016-08-08 18:44:38 +01006702 LOG_API(i_isolate, ArrayBuffer, New);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006703 ENTER_V8(i_isolate);
6704 i::Handle<i::JSArrayBuffer> obj =
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006705 i_isolate->factory()->NewJSArrayBuffer(i::SharedFlag::kNotShared);
6706 i::JSArrayBuffer::SetupAllocatingData(obj, i_isolate, byte_length);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006707 return Utils::ToLocal(obj);
6708}
6709
6710
6711Local<ArrayBuffer> v8::ArrayBuffer::New(Isolate* isolate, void* data,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006712 size_t byte_length,
6713 ArrayBufferCreationMode mode) {
6714 // Embedders must guarantee that the external backing store is valid.
6715 CHECK(byte_length == 0 || data != NULL);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006716 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
Ben Murdochc5610432016-08-08 18:44:38 +01006717 LOG_API(i_isolate, ArrayBuffer, New);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006718 ENTER_V8(i_isolate);
6719 i::Handle<i::JSArrayBuffer> obj =
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006720 i_isolate->factory()->NewJSArrayBuffer(i::SharedFlag::kNotShared);
6721 i::JSArrayBuffer::Setup(obj, i_isolate,
6722 mode == ArrayBufferCreationMode::kExternalized, data,
6723 byte_length);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006724 return Utils::ToLocal(obj);
6725}
6726
6727
6728Local<ArrayBuffer> v8::ArrayBufferView::Buffer() {
6729 i::Handle<i::JSArrayBufferView> obj = Utils::OpenHandle(this);
6730 i::Handle<i::JSArrayBuffer> buffer;
6731 if (obj->IsJSDataView()) {
6732 i::Handle<i::JSDataView> data_view(i::JSDataView::cast(*obj));
6733 DCHECK(data_view->buffer()->IsJSArrayBuffer());
6734 buffer = i::handle(i::JSArrayBuffer::cast(data_view->buffer()));
6735 } else {
6736 DCHECK(obj->IsJSTypedArray());
6737 buffer = i::JSTypedArray::cast(*obj)->GetBuffer();
6738 }
6739 return Utils::ToLocal(buffer);
6740}
6741
6742
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006743size_t v8::ArrayBufferView::CopyContents(void* dest, size_t byte_length) {
6744 i::Handle<i::JSArrayBufferView> self = Utils::OpenHandle(this);
6745 i::Isolate* isolate = self->GetIsolate();
6746 size_t byte_offset = i::NumberToSize(isolate, self->byte_offset());
6747 size_t bytes_to_copy =
6748 i::Min(byte_length, i::NumberToSize(isolate, self->byte_length()));
6749 if (bytes_to_copy) {
6750 i::DisallowHeapAllocation no_gc;
6751 i::Handle<i::JSArrayBuffer> buffer(i::JSArrayBuffer::cast(self->buffer()));
6752 const char* source = reinterpret_cast<char*>(buffer->backing_store());
6753 if (source == nullptr) {
6754 DCHECK(self->IsJSTypedArray());
6755 i::Handle<i::JSTypedArray> typed_array(i::JSTypedArray::cast(*self));
6756 i::Handle<i::FixedTypedArrayBase> fixed_array(
6757 i::FixedTypedArrayBase::cast(typed_array->elements()));
6758 source = reinterpret_cast<char*>(fixed_array->DataPtr());
6759 }
6760 memcpy(dest, source + byte_offset, bytes_to_copy);
6761 }
6762 return bytes_to_copy;
6763}
6764
6765
6766bool v8::ArrayBufferView::HasBuffer() const {
6767 i::Handle<i::JSArrayBufferView> self = Utils::OpenHandle(this);
6768 i::Handle<i::JSArrayBuffer> buffer(i::JSArrayBuffer::cast(self->buffer()));
6769 return buffer->backing_store() != nullptr;
6770}
6771
6772
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006773size_t v8::ArrayBufferView::ByteOffset() {
6774 i::Handle<i::JSArrayBufferView> obj = Utils::OpenHandle(this);
6775 return static_cast<size_t>(obj->byte_offset()->Number());
6776}
6777
6778
6779size_t v8::ArrayBufferView::ByteLength() {
6780 i::Handle<i::JSArrayBufferView> obj = Utils::OpenHandle(this);
6781 return static_cast<size_t>(obj->byte_length()->Number());
6782}
6783
6784
6785size_t v8::TypedArray::Length() {
6786 i::Handle<i::JSTypedArray> obj = Utils::OpenHandle(this);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006787 return static_cast<size_t>(obj->length_value());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006788}
6789
Ben Murdochc5610432016-08-08 18:44:38 +01006790#define TYPED_ARRAY_NEW(Type, type, TYPE, ctype, size) \
6791 Local<Type##Array> Type##Array::New(Local<ArrayBuffer> array_buffer, \
6792 size_t byte_offset, size_t length) { \
6793 i::Isolate* isolate = Utils::OpenHandle(*array_buffer)->GetIsolate(); \
6794 LOG_API(isolate, Type##Array, New); \
6795 ENTER_V8(isolate); \
6796 if (!Utils::ApiCheck(length <= static_cast<size_t>(i::Smi::kMaxValue), \
6797 "v8::" #Type \
6798 "Array::New(Local<ArrayBuffer>, size_t, size_t)", \
6799 "length exceeds max allowed value")) { \
6800 return Local<Type##Array>(); \
6801 } \
6802 i::Handle<i::JSArrayBuffer> buffer = Utils::OpenHandle(*array_buffer); \
6803 i::Handle<i::JSTypedArray> obj = isolate->factory()->NewJSTypedArray( \
6804 i::kExternal##Type##Array, buffer, byte_offset, length); \
6805 return Utils::ToLocal##Type##Array(obj); \
6806 } \
6807 Local<Type##Array> Type##Array::New( \
6808 Local<SharedArrayBuffer> shared_array_buffer, size_t byte_offset, \
6809 size_t length) { \
6810 CHECK(i::FLAG_harmony_sharedarraybuffer); \
6811 i::Isolate* isolate = \
6812 Utils::OpenHandle(*shared_array_buffer)->GetIsolate(); \
6813 LOG_API(isolate, Type##Array, New); \
6814 ENTER_V8(isolate); \
6815 if (!Utils::ApiCheck( \
6816 length <= static_cast<size_t>(i::Smi::kMaxValue), \
6817 "v8::" #Type \
6818 "Array::New(Local<SharedArrayBuffer>, size_t, size_t)", \
6819 "length exceeds max allowed value")) { \
6820 return Local<Type##Array>(); \
6821 } \
6822 i::Handle<i::JSArrayBuffer> buffer = \
6823 Utils::OpenHandle(*shared_array_buffer); \
6824 i::Handle<i::JSTypedArray> obj = isolate->factory()->NewJSTypedArray( \
6825 i::kExternal##Type##Array, buffer, byte_offset, length); \
6826 return Utils::ToLocal##Type##Array(obj); \
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006827 }
6828
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006829TYPED_ARRAYS(TYPED_ARRAY_NEW)
6830#undef TYPED_ARRAY_NEW
6831
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006832Local<DataView> DataView::New(Local<ArrayBuffer> array_buffer,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006833 size_t byte_offset, size_t byte_length) {
6834 i::Handle<i::JSArrayBuffer> buffer = Utils::OpenHandle(*array_buffer);
6835 i::Isolate* isolate = buffer->GetIsolate();
Ben Murdochc5610432016-08-08 18:44:38 +01006836 LOG_API(isolate, DataView, New);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006837 ENTER_V8(isolate);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04006838 i::Handle<i::JSDataView> obj =
6839 isolate->factory()->NewJSDataView(buffer, byte_offset, byte_length);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006840 return Utils::ToLocal(obj);
6841}
6842
6843
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006844Local<DataView> DataView::New(Local<SharedArrayBuffer> shared_array_buffer,
6845 size_t byte_offset, size_t byte_length) {
6846 CHECK(i::FLAG_harmony_sharedarraybuffer);
6847 i::Handle<i::JSArrayBuffer> buffer = Utils::OpenHandle(*shared_array_buffer);
6848 i::Isolate* isolate = buffer->GetIsolate();
Ben Murdochc5610432016-08-08 18:44:38 +01006849 LOG_API(isolate, DataView, New);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006850 ENTER_V8(isolate);
6851 i::Handle<i::JSDataView> obj =
6852 isolate->factory()->NewJSDataView(buffer, byte_offset, byte_length);
6853 return Utils::ToLocal(obj);
6854}
6855
6856
6857bool v8::SharedArrayBuffer::IsExternal() const {
6858 return Utils::OpenHandle(this)->is_external();
6859}
6860
6861
6862v8::SharedArrayBuffer::Contents v8::SharedArrayBuffer::Externalize() {
6863 i::Handle<i::JSArrayBuffer> self = Utils::OpenHandle(this);
6864 i::Isolate* isolate = self->GetIsolate();
Ben Murdochc5610432016-08-08 18:44:38 +01006865 Utils::ApiCheck(!self->is_external(), "v8_SharedArrayBuffer_Externalize",
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006866 "SharedArrayBuffer already externalized");
6867 self->set_is_external(true);
6868 isolate->heap()->UnregisterArrayBuffer(*self);
6869 return GetContents();
6870}
6871
6872
6873v8::SharedArrayBuffer::Contents v8::SharedArrayBuffer::GetContents() {
6874 i::Handle<i::JSArrayBuffer> self = Utils::OpenHandle(this);
6875 size_t byte_length = static_cast<size_t>(self->byte_length()->Number());
6876 Contents contents;
6877 contents.data_ = self->backing_store();
6878 contents.byte_length_ = byte_length;
6879 return contents;
6880}
6881
6882
6883size_t v8::SharedArrayBuffer::ByteLength() const {
6884 i::Handle<i::JSArrayBuffer> obj = Utils::OpenHandle(this);
6885 return static_cast<size_t>(obj->byte_length()->Number());
6886}
6887
6888
6889Local<SharedArrayBuffer> v8::SharedArrayBuffer::New(Isolate* isolate,
6890 size_t byte_length) {
6891 CHECK(i::FLAG_harmony_sharedarraybuffer);
6892 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
Ben Murdochc5610432016-08-08 18:44:38 +01006893 LOG_API(i_isolate, SharedArrayBuffer, New);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006894 ENTER_V8(i_isolate);
6895 i::Handle<i::JSArrayBuffer> obj =
6896 i_isolate->factory()->NewJSArrayBuffer(i::SharedFlag::kShared);
6897 i::JSArrayBuffer::SetupAllocatingData(obj, i_isolate, byte_length, true,
6898 i::SharedFlag::kShared);
6899 return Utils::ToLocalShared(obj);
6900}
6901
6902
6903Local<SharedArrayBuffer> v8::SharedArrayBuffer::New(
6904 Isolate* isolate, void* data, size_t byte_length,
6905 ArrayBufferCreationMode mode) {
6906 CHECK(i::FLAG_harmony_sharedarraybuffer);
6907 // Embedders must guarantee that the external backing store is valid.
6908 CHECK(byte_length == 0 || data != NULL);
6909 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
Ben Murdochc5610432016-08-08 18:44:38 +01006910 LOG_API(i_isolate, SharedArrayBuffer, New);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006911 ENTER_V8(i_isolate);
6912 i::Handle<i::JSArrayBuffer> obj =
6913 i_isolate->factory()->NewJSArrayBuffer(i::SharedFlag::kShared);
6914 i::JSArrayBuffer::Setup(obj, i_isolate,
6915 mode == ArrayBufferCreationMode::kExternalized, data,
6916 byte_length, i::SharedFlag::kShared);
6917 return Utils::ToLocalShared(obj);
6918}
6919
6920
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006921Local<Symbol> v8::Symbol::New(Isolate* isolate, Local<String> name) {
6922 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
Ben Murdochc5610432016-08-08 18:44:38 +01006923 LOG_API(i_isolate, Symbol, New);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006924 ENTER_V8(i_isolate);
6925 i::Handle<i::Symbol> result = i_isolate->factory()->NewSymbol();
6926 if (!name.IsEmpty()) result->set_name(*Utils::OpenHandle(*name));
Steve Blocka7e24c12009-10-30 11:49:00 +00006927 return Utils::ToLocal(result);
6928}
6929
6930
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006931static i::Handle<i::Symbol> SymbolFor(i::Isolate* isolate,
6932 i::Handle<i::String> name,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006933 i::Handle<i::String> part,
6934 bool private_symbol) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006935 i::Handle<i::JSObject> registry = isolate->GetSymbolRegistry();
6936 i::Handle<i::JSObject> symbols =
6937 i::Handle<i::JSObject>::cast(
6938 i::Object::GetPropertyOrElement(registry, part).ToHandleChecked());
6939 i::Handle<i::Object> symbol =
6940 i::Object::GetPropertyOrElement(symbols, name).ToHandleChecked();
6941 if (!symbol->IsSymbol()) {
6942 DCHECK(symbol->IsUndefined());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006943 if (private_symbol)
6944 symbol = isolate->factory()->NewPrivateSymbol();
6945 else
6946 symbol = isolate->factory()->NewSymbol();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006947 i::Handle<i::Symbol>::cast(symbol)->set_name(*name);
Ben Murdochda12d292016-06-02 14:46:10 +01006948 i::Object::SetPropertyOrElement(symbols, name, symbol, i::STRICT).Assert();
Steve Blockd0582a62009-12-15 09:54:21 +00006949 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006950 return i::Handle<i::Symbol>::cast(symbol);
6951}
6952
6953
6954Local<Symbol> v8::Symbol::For(Isolate* isolate, Local<String> name) {
6955 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
6956 i::Handle<i::String> i_name = Utils::OpenHandle(*name);
6957 i::Handle<i::String> part = i_isolate->factory()->for_string();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006958 return Utils::ToLocal(SymbolFor(i_isolate, i_name, part, false));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006959}
6960
6961
6962Local<Symbol> v8::Symbol::ForApi(Isolate* isolate, Local<String> name) {
6963 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
6964 i::Handle<i::String> i_name = Utils::OpenHandle(*name);
6965 i::Handle<i::String> part = i_isolate->factory()->for_api_string();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006966 return Utils::ToLocal(SymbolFor(i_isolate, i_name, part, false));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006967}
6968
6969
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006970Local<Symbol> v8::Symbol::GetIterator(Isolate* isolate) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04006971 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
6972 return Utils::ToLocal(i_isolate->factory()->iterator_symbol());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006973}
6974
6975
6976Local<Symbol> v8::Symbol::GetUnscopables(Isolate* isolate) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04006977 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
6978 return Utils::ToLocal(i_isolate->factory()->unscopables_symbol());
6979}
6980
6981
6982Local<Symbol> v8::Symbol::GetToStringTag(Isolate* isolate) {
6983 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
6984 return Utils::ToLocal(i_isolate->factory()->to_string_tag_symbol());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006985}
6986
6987
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006988Local<Symbol> v8::Symbol::GetIsConcatSpreadable(Isolate* isolate) {
6989 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
6990 return Utils::ToLocal(i_isolate->factory()->is_concat_spreadable_symbol());
6991}
6992
6993
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006994Local<Private> v8::Private::New(Isolate* isolate, Local<String> name) {
6995 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
Ben Murdochc5610432016-08-08 18:44:38 +01006996 LOG_API(i_isolate, Private, New);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006997 ENTER_V8(i_isolate);
6998 i::Handle<i::Symbol> symbol = i_isolate->factory()->NewPrivateSymbol();
6999 if (!name.IsEmpty()) symbol->set_name(*Utils::OpenHandle(*name));
7000 Local<Symbol> result = Utils::ToLocal(symbol);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007001 return v8::Local<Private>(reinterpret_cast<Private*>(*result));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007002}
7003
7004
7005Local<Private> v8::Private::ForApi(Isolate* isolate, Local<String> name) {
7006 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
7007 i::Handle<i::String> i_name = Utils::OpenHandle(*name);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007008 i::Handle<i::String> part = i_isolate->factory()->private_api_string();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007009 Local<Symbol> result =
7010 Utils::ToLocal(SymbolFor(i_isolate, i_name, part, true));
7011 return v8::Local<Private>(reinterpret_cast<Private*>(*result));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007012}
7013
7014
7015Local<Number> v8::Number::New(Isolate* isolate, double value) {
7016 i::Isolate* internal_isolate = reinterpret_cast<i::Isolate*>(isolate);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007017 if (std::isnan(value)) {
7018 // Introduce only canonical NaN value into the VM, to avoid signaling NaNs.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007019 value = std::numeric_limits<double>::quiet_NaN();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007020 }
7021 ENTER_V8(internal_isolate);
7022 i::Handle<i::Object> result = internal_isolate->factory()->NewNumber(value);
Steve Blocka7e24c12009-10-30 11:49:00 +00007023 return Utils::NumberToLocal(result);
7024}
7025
7026
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007027Local<Integer> v8::Integer::New(Isolate* isolate, int32_t value) {
7028 i::Isolate* internal_isolate = reinterpret_cast<i::Isolate*>(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00007029 if (i::Smi::IsValid(value)) {
Steve Block44f0eee2011-05-26 01:26:41 +01007030 return Utils::IntegerToLocal(i::Handle<i::Object>(i::Smi::FromInt(value),
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007031 internal_isolate));
Steve Blocka7e24c12009-10-30 11:49:00 +00007032 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007033 ENTER_V8(internal_isolate);
7034 i::Handle<i::Object> result = internal_isolate->factory()->NewNumber(value);
Steve Blocka7e24c12009-10-30 11:49:00 +00007035 return Utils::IntegerToLocal(result);
7036}
7037
7038
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007039Local<Integer> v8::Integer::NewFromUnsigned(Isolate* isolate, uint32_t value) {
7040 i::Isolate* internal_isolate = reinterpret_cast<i::Isolate*>(isolate);
Steve Block3ce2e202009-11-05 08:53:23 +00007041 bool fits_into_int32_t = (value & (1 << 31)) == 0;
7042 if (fits_into_int32_t) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007043 return Integer::New(isolate, static_cast<int32_t>(value));
Steve Block3ce2e202009-11-05 08:53:23 +00007044 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007045 ENTER_V8(internal_isolate);
7046 i::Handle<i::Object> result = internal_isolate->factory()->NewNumber(value);
Steve Block3ce2e202009-11-05 08:53:23 +00007047 return Utils::IntegerToLocal(result);
7048}
7049
7050
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007051void Isolate::ReportExternalAllocationLimitReached() {
7052 i::Heap* heap = reinterpret_cast<i::Isolate*>(this)->heap();
7053 if (heap->gc_state() != i::Heap::NOT_IN_GC) return;
7054 heap->ReportExternalMemoryPressure(
7055 "external memory allocation limit reached.");
Steve Blocka7e24c12009-10-30 11:49:00 +00007056}
7057
7058
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007059HeapProfiler* Isolate::GetHeapProfiler() {
7060 i::HeapProfiler* heap_profiler =
7061 reinterpret_cast<i::Isolate*>(this)->heap_profiler();
7062 return reinterpret_cast<HeapProfiler*>(heap_profiler);
Steve Blocka7e24c12009-10-30 11:49:00 +00007063}
7064
7065
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007066CpuProfiler* Isolate::GetCpuProfiler() {
7067 i::CpuProfiler* cpu_profiler =
7068 reinterpret_cast<i::Isolate*>(this)->cpu_profiler();
7069 return reinterpret_cast<CpuProfiler*>(cpu_profiler);
7070}
7071
7072
7073bool Isolate::InContext() {
7074 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
7075 return isolate->context() != NULL;
7076}
7077
7078
7079v8::Local<v8::Context> Isolate::GetCurrentContext() {
7080 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
7081 i::Context* context = isolate->context();
7082 if (context == NULL) return Local<Context>();
7083 i::Context* native_context = context->native_context();
7084 if (native_context == NULL) return Local<Context>();
7085 return Utils::ToLocal(i::Handle<i::Context>(native_context));
7086}
7087
7088
7089v8::Local<v8::Context> Isolate::GetCallingContext() {
7090 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
7091 i::Handle<i::Object> calling = isolate->GetCallingNativeContext();
7092 if (calling.is_null()) return Local<Context>();
7093 return Utils::ToLocal(i::Handle<i::Context>::cast(calling));
7094}
7095
7096
7097v8::Local<v8::Context> Isolate::GetEnteredContext() {
7098 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
7099 i::Handle<i::Object> last =
7100 isolate->handle_scope_implementer()->LastEnteredContext();
7101 if (last.is_null()) return Local<Context>();
7102 return Utils::ToLocal(i::Handle<i::Context>::cast(last));
7103}
7104
7105
7106v8::Local<Value> Isolate::ThrowException(v8::Local<v8::Value> value) {
7107 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
7108 ENTER_V8(isolate);
7109 // If we're passed an empty handle, we throw an undefined exception
7110 // to deal more gracefully with out of memory situations.
7111 if (value.IsEmpty()) {
7112 isolate->ScheduleThrow(isolate->heap()->undefined_value());
7113 } else {
7114 isolate->ScheduleThrow(*Utils::OpenHandle(*value));
Steve Block44f0eee2011-05-26 01:26:41 +01007115 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007116 return v8::Undefined(reinterpret_cast<v8::Isolate*>(isolate));
Steve Blocka7e24c12009-10-30 11:49:00 +00007117}
7118
7119
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007120void Isolate::SetObjectGroupId(internal::Object** object, UniqueId id) {
7121 i::Isolate* internal_isolate = reinterpret_cast<i::Isolate*>(this);
7122 internal_isolate->global_handles()->SetObjectGroupId(
7123 v8::internal::Handle<v8::internal::Object>(object).location(),
7124 id);
Steve Blocka7e24c12009-10-30 11:49:00 +00007125}
7126
7127
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007128void Isolate::SetReferenceFromGroup(UniqueId id, internal::Object** object) {
7129 i::Isolate* internal_isolate = reinterpret_cast<i::Isolate*>(this);
7130 internal_isolate->global_handles()->SetReferenceFromGroup(
7131 id,
7132 v8::internal::Handle<v8::internal::Object>(object).location());
7133}
7134
7135
7136void Isolate::SetReference(internal::Object** parent,
7137 internal::Object** child) {
7138 i::Isolate* internal_isolate = reinterpret_cast<i::Isolate*>(this);
7139 i::Object** parent_location =
7140 v8::internal::Handle<v8::internal::Object>(parent).location();
7141 internal_isolate->global_handles()->SetReference(
7142 reinterpret_cast<i::HeapObject**>(parent_location),
7143 v8::internal::Handle<v8::internal::Object>(child).location());
7144}
7145
7146
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007147void Isolate::AddGCPrologueCallback(GCCallback callback, GCType gc_type) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007148 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
7149 isolate->heap()->AddGCPrologueCallback(callback, gc_type);
7150}
7151
7152
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007153void Isolate::RemoveGCPrologueCallback(GCCallback callback) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007154 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
7155 isolate->heap()->RemoveGCPrologueCallback(callback);
7156}
7157
7158
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007159void Isolate::AddGCEpilogueCallback(GCCallback callback, GCType gc_type) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007160 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
7161 isolate->heap()->AddGCEpilogueCallback(callback, gc_type);
7162}
7163
7164
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007165void Isolate::RemoveGCEpilogueCallback(GCCallback callback) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007166 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
7167 isolate->heap()->RemoveGCEpilogueCallback(callback);
Steve Blocka7e24c12009-10-30 11:49:00 +00007168}
7169
7170
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007171void V8::AddGCPrologueCallback(GCCallback callback, GCType gc_type) {
Steve Block44f0eee2011-05-26 01:26:41 +01007172 i::Isolate* isolate = i::Isolate::Current();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007173 isolate->heap()->AddGCPrologueCallback(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007174 reinterpret_cast<v8::Isolate::GCCallback>(callback), gc_type, false);
Steve Block6ded16b2010-05-10 14:33:55 +01007175}
7176
7177
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007178void V8::AddGCEpilogueCallback(GCCallback callback, GCType gc_type) {
Steve Block44f0eee2011-05-26 01:26:41 +01007179 i::Isolate* isolate = i::Isolate::Current();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007180 isolate->heap()->AddGCEpilogueCallback(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007181 reinterpret_cast<v8::Isolate::GCCallback>(callback), gc_type, false);
Steve Block6ded16b2010-05-10 14:33:55 +01007182}
7183
Ben Murdochda12d292016-06-02 14:46:10 +01007184void Isolate::SetEmbedderHeapTracer(EmbedderHeapTracer* tracer) {
7185 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
7186 isolate->heap()->SetEmbedderHeapTracer(tracer);
7187}
Steve Block6ded16b2010-05-10 14:33:55 +01007188
Emily Bernierd0a1eb72015-03-24 16:35:39 -04007189void Isolate::AddMemoryAllocationCallback(MemoryAllocationCallback callback,
7190 ObjectSpace space,
7191 AllocationAction action) {
7192 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
Ben Murdochc5610432016-08-08 18:44:38 +01007193 isolate->heap()->memory_allocator()->AddMemoryAllocationCallback(
Steve Block44f0eee2011-05-26 01:26:41 +01007194 callback, space, action);
Iain Merrick9ac36c92010-09-13 15:29:50 +01007195}
7196
7197
Emily Bernierd0a1eb72015-03-24 16:35:39 -04007198void Isolate::RemoveMemoryAllocationCallback(
7199 MemoryAllocationCallback callback) {
7200 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
Ben Murdochc5610432016-08-08 18:44:38 +01007201 isolate->heap()->memory_allocator()->RemoveMemoryAllocationCallback(callback);
Iain Merrick9ac36c92010-09-13 15:29:50 +01007202}
7203
7204
Emily Bernierd0a1eb72015-03-24 16:35:39 -04007205void Isolate::TerminateExecution() {
7206 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
7207 isolate->stack_guard()->RequestTerminateExecution();
Steve Blocka7e24c12009-10-30 11:49:00 +00007208}
7209
7210
Emily Bernierd0a1eb72015-03-24 16:35:39 -04007211bool Isolate::IsExecutionTerminating() {
7212 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
7213 return IsExecutionTerminatingCheck(isolate);
Steve Block44f0eee2011-05-26 01:26:41 +01007214}
7215
7216
Emily Bernierd0a1eb72015-03-24 16:35:39 -04007217void Isolate::CancelTerminateExecution() {
7218 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
7219 isolate->stack_guard()->ClearTerminateExecution();
7220 isolate->CancelTerminateExecution();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007221}
7222
7223
7224void Isolate::RequestInterrupt(InterruptCallback callback, void* data) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04007225 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
7226 isolate->RequestInterrupt(callback, data);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007227}
7228
7229
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007230void Isolate::RequestGarbageCollectionForTesting(GarbageCollectionType type) {
7231 CHECK(i::FLAG_expose_gc);
7232 if (type == kMinorGarbageCollection) {
7233 reinterpret_cast<i::Isolate*>(this)->heap()->CollectGarbage(
7234 i::NEW_SPACE, "Isolate::RequestGarbageCollection",
7235 kGCCallbackFlagForced);
7236 } else {
7237 DCHECK_EQ(kFullGarbageCollection, type);
7238 reinterpret_cast<i::Isolate*>(this)->heap()->CollectAllGarbage(
7239 i::Heap::kAbortIncrementalMarkingMask,
7240 "Isolate::RequestGarbageCollection", kGCCallbackFlagForced);
7241 }
7242}
7243
7244
Steve Block44f0eee2011-05-26 01:26:41 +01007245Isolate* Isolate::GetCurrent() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007246 i::Isolate* isolate = i::Isolate::Current();
Steve Block44f0eee2011-05-26 01:26:41 +01007247 return reinterpret_cast<Isolate*>(isolate);
7248}
7249
7250
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007251Isolate* Isolate::New(const Isolate::CreateParams& params) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007252 i::Isolate* isolate = new i::Isolate(false);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007253 Isolate* v8_isolate = reinterpret_cast<Isolate*>(isolate);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007254 CHECK(params.array_buffer_allocator != NULL);
7255 isolate->set_array_buffer_allocator(params.array_buffer_allocator);
7256 if (params.snapshot_blob != NULL) {
7257 isolate->set_snapshot_blob(params.snapshot_blob);
7258 } else {
7259 isolate->set_snapshot_blob(i::Snapshot::DefaultSnapshotBlob());
7260 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007261 if (params.entry_hook) {
7262 isolate->set_function_entry_hook(params.entry_hook);
7263 }
Ben Murdoch097c5b22016-05-18 11:27:45 +01007264 auto code_event_handler = params.code_event_handler;
7265#ifdef ENABLE_GDB_JIT_INTERFACE
7266 if (code_event_handler == nullptr && i::FLAG_gdbjit) {
7267 code_event_handler = i::GDBJITInterface::EventHandler;
7268 }
7269#endif // ENABLE_GDB_JIT_INTERFACE
7270 if (code_event_handler) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007271 isolate->InitializeLoggingAndCounters();
7272 isolate->logger()->SetCodeEventHandler(kJitCodeEventDefault,
Ben Murdoch097c5b22016-05-18 11:27:45 +01007273 code_event_handler);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007274 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007275 if (params.counter_lookup_callback) {
7276 v8_isolate->SetCounterFunction(params.counter_lookup_callback);
7277 }
7278
7279 if (params.create_histogram_callback) {
7280 v8_isolate->SetCreateHistogramFunction(params.create_histogram_callback);
7281 }
7282
7283 if (params.add_histogram_sample_callback) {
7284 v8_isolate->SetAddHistogramSampleFunction(
7285 params.add_histogram_sample_callback);
7286 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007287 SetResourceConstraints(isolate, params.constraints);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007288 // TODO(jochen): Once we got rid of Isolate::Current(), we can remove this.
7289 Isolate::Scope isolate_scope(v8_isolate);
7290 if (params.entry_hook || !i::Snapshot::Initialize(isolate)) {
7291 // If the isolate has a function entry hook, it needs to re-build all its
7292 // code stubs with entry hooks embedded, so don't deserialize a snapshot.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007293 if (i::Snapshot::EmbedsScript(isolate)) {
7294 // If the snapshot embeds a script, we cannot initialize the isolate
7295 // without the snapshot as a fallback. This is unlikely to happen though.
7296 V8_Fatal(__FILE__, __LINE__,
7297 "Initializing isolate from custom startup snapshot failed");
7298 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007299 isolate->Init(NULL);
7300 }
7301 return v8_isolate;
Steve Block44f0eee2011-05-26 01:26:41 +01007302}
7303
7304
7305void Isolate::Dispose() {
7306 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007307 if (!Utils::ApiCheck(!isolate->IsInUse(),
7308 "v8::Isolate::Dispose()",
7309 "Disposing the isolate that is entered by a thread.")) {
Steve Block44f0eee2011-05-26 01:26:41 +01007310 return;
Steve Block6ded16b2010-05-10 14:33:55 +01007311 }
Steve Block44f0eee2011-05-26 01:26:41 +01007312 isolate->TearDown();
7313}
7314
7315
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007316void Isolate::DiscardThreadSpecificMetadata() {
7317 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
7318 isolate->DiscardPerThreadDataForThisThread();
7319}
7320
7321
Steve Block44f0eee2011-05-26 01:26:41 +01007322void Isolate::Enter() {
7323 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
7324 isolate->Enter();
7325}
7326
7327
7328void Isolate::Exit() {
7329 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
7330 isolate->Exit();
Steve Block6ded16b2010-05-10 14:33:55 +01007331}
7332
7333
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007334void Isolate::SetAbortOnUncaughtExceptionCallback(
7335 AbortOnUncaughtExceptionCallback callback) {
7336 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
7337 isolate->SetAbortOnUncaughtExceptionCallback(callback);
7338}
7339
7340
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007341Isolate::DisallowJavascriptExecutionScope::DisallowJavascriptExecutionScope(
7342 Isolate* isolate,
7343 Isolate::DisallowJavascriptExecutionScope::OnFailure on_failure)
7344 : on_failure_(on_failure) {
7345 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
7346 if (on_failure_ == CRASH_ON_FAILURE) {
7347 internal_ = reinterpret_cast<void*>(
7348 new i::DisallowJavascriptExecution(i_isolate));
7349 } else {
7350 DCHECK_EQ(THROW_ON_FAILURE, on_failure);
7351 internal_ = reinterpret_cast<void*>(
7352 new i::ThrowOnJavascriptExecution(i_isolate));
7353 }
Ben Murdoch257744e2011-11-30 15:57:28 +00007354}
7355
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007356
7357Isolate::DisallowJavascriptExecutionScope::~DisallowJavascriptExecutionScope() {
7358 if (on_failure_ == CRASH_ON_FAILURE) {
7359 delete reinterpret_cast<i::DisallowJavascriptExecution*>(internal_);
7360 } else {
7361 delete reinterpret_cast<i::ThrowOnJavascriptExecution*>(internal_);
7362 }
7363}
7364
7365
7366Isolate::AllowJavascriptExecutionScope::AllowJavascriptExecutionScope(
7367 Isolate* isolate) {
7368 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
7369 internal_assert_ = reinterpret_cast<void*>(
7370 new i::AllowJavascriptExecution(i_isolate));
7371 internal_throws_ = reinterpret_cast<void*>(
7372 new i::NoThrowOnJavascriptExecution(i_isolate));
7373}
7374
7375
7376Isolate::AllowJavascriptExecutionScope::~AllowJavascriptExecutionScope() {
7377 delete reinterpret_cast<i::AllowJavascriptExecution*>(internal_assert_);
7378 delete reinterpret_cast<i::NoThrowOnJavascriptExecution*>(internal_throws_);
7379}
7380
7381
7382Isolate::SuppressMicrotaskExecutionScope::SuppressMicrotaskExecutionScope(
7383 Isolate* isolate)
7384 : isolate_(reinterpret_cast<i::Isolate*>(isolate)) {
7385 isolate_->handle_scope_implementer()->IncrementCallDepth();
Ben Murdochda12d292016-06-02 14:46:10 +01007386 isolate_->handle_scope_implementer()->IncrementMicrotasksSuppressions();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007387}
7388
7389
7390Isolate::SuppressMicrotaskExecutionScope::~SuppressMicrotaskExecutionScope() {
Ben Murdochda12d292016-06-02 14:46:10 +01007391 isolate_->handle_scope_implementer()->DecrementMicrotasksSuppressions();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007392 isolate_->handle_scope_implementer()->DecrementCallDepth();
7393}
7394
7395
7396void Isolate::GetHeapStatistics(HeapStatistics* heap_statistics) {
Ben Murdoch257744e2011-11-30 15:57:28 +00007397 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007398 i::Heap* heap = isolate->heap();
7399 heap_statistics->total_heap_size_ = heap->CommittedMemory();
7400 heap_statistics->total_heap_size_executable_ =
7401 heap->CommittedMemoryExecutable();
7402 heap_statistics->total_physical_size_ = heap->CommittedPhysicalMemory();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007403 heap_statistics->total_available_size_ = heap->Available();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007404 heap_statistics->used_heap_size_ = heap->SizeOfObjects();
7405 heap_statistics->heap_size_limit_ = heap->MaxReserved();
Ben Murdochda12d292016-06-02 14:46:10 +01007406 heap_statistics->malloced_memory_ =
7407 isolate->allocator()->GetCurrentMemoryUsage();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007408 heap_statistics->does_zap_garbage_ = heap->ShouldZapGarbage();
7409}
7410
7411
7412size_t Isolate::NumberOfHeapSpaces() {
7413 return i::LAST_SPACE - i::FIRST_SPACE + 1;
7414}
7415
7416
7417bool Isolate::GetHeapSpaceStatistics(HeapSpaceStatistics* space_statistics,
7418 size_t index) {
7419 if (!space_statistics) return false;
7420 if (!i::Heap::IsValidAllocationSpace(static_cast<i::AllocationSpace>(index)))
7421 return false;
7422
7423 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
7424 i::Heap* heap = isolate->heap();
7425 i::Space* space = heap->space(static_cast<int>(index));
7426
7427 space_statistics->space_name_ = heap->GetSpaceName(static_cast<int>(index));
7428 space_statistics->space_size_ = space->CommittedMemory();
7429 space_statistics->space_used_size_ = space->SizeOfObjects();
7430 space_statistics->space_available_size_ = space->Available();
7431 space_statistics->physical_space_size_ = space->CommittedPhysicalMemory();
7432 return true;
7433}
7434
7435
7436size_t Isolate::NumberOfTrackedHeapObjectTypes() {
7437 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
7438 i::Heap* heap = isolate->heap();
7439 return heap->NumberOfTrackedHeapObjectTypes();
7440}
7441
7442
7443bool Isolate::GetHeapObjectStatisticsAtLastGC(
7444 HeapObjectStatistics* object_statistics, size_t type_index) {
7445 if (!object_statistics) return false;
7446 if (!i::FLAG_track_gc_object_stats) return false;
7447
7448 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
7449 i::Heap* heap = isolate->heap();
7450 if (type_index >= heap->NumberOfTrackedHeapObjectTypes()) return false;
7451
7452 const char* object_type;
7453 const char* object_sub_type;
7454 size_t object_count = heap->ObjectCountAtLastGC(type_index);
7455 size_t object_size = heap->ObjectSizeAtLastGC(type_index);
7456 if (!heap->GetObjectTypeName(type_index, &object_type, &object_sub_type)) {
7457 // There should be no objects counted when the type is unknown.
7458 DCHECK_EQ(object_count, 0U);
7459 DCHECK_EQ(object_size, 0U);
7460 return false;
7461 }
7462
7463 object_statistics->object_type_ = object_type;
7464 object_statistics->object_sub_type_ = object_sub_type;
7465 object_statistics->object_count_ = object_count;
7466 object_statistics->object_size_ = object_size;
7467 return true;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007468}
7469
7470
Emily Bernierd0a1eb72015-03-24 16:35:39 -04007471void Isolate::GetStackSample(const RegisterState& state, void** frames,
7472 size_t frames_limit, SampleInfo* sample_info) {
7473 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
Ben Murdochc5610432016-08-08 18:44:38 +01007474#if defined(USE_SIMULATOR)
7475 RegisterState regs;
7476 regs.pc = state.pc;
7477 regs.sp = state.sp;
7478 regs.fp = state.fp;
7479 i::SimulatorHelper::FillRegisters(isolate, &regs);
7480#else
7481 const RegisterState& regs = state;
7482#endif
7483 i::TickSample::GetStackSample(isolate, regs, i::TickSample::kSkipCEntryFrame,
Emily Bernierd0a1eb72015-03-24 16:35:39 -04007484 frames, frames_limit, sample_info);
7485}
7486
Ben Murdochc5610432016-08-08 18:44:38 +01007487size_t Isolate::NumberOfPhantomHandleResetsSinceLastCall() {
7488 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
7489 size_t result = isolate->global_handles()->NumberOfPhantomHandleResets();
7490 isolate->global_handles()->ResetNumberOfPhantomHandleResets();
7491 return result;
7492}
Emily Bernierd0a1eb72015-03-24 16:35:39 -04007493
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007494void Isolate::SetEventLogger(LogEventCallback that) {
7495 // Do not overwrite the event logger if we want to log explicitly.
Emily Bernierd0a1eb72015-03-24 16:35:39 -04007496 if (i::FLAG_log_internal_timer_events) return;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007497 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
7498 isolate->set_event_logger(that);
7499}
7500
7501
Ben Murdoch097c5b22016-05-18 11:27:45 +01007502void Isolate::AddBeforeCallEnteredCallback(BeforeCallEnteredCallback callback) {
7503 if (callback == NULL) return;
7504 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
7505 isolate->AddBeforeCallEnteredCallback(callback);
7506}
7507
7508
7509void Isolate::RemoveBeforeCallEnteredCallback(
7510 BeforeCallEnteredCallback callback) {
7511 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
7512 isolate->RemoveBeforeCallEnteredCallback(callback);
7513}
7514
7515
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007516void Isolate::AddCallCompletedCallback(CallCompletedCallback callback) {
7517 if (callback == NULL) return;
7518 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
7519 isolate->AddCallCompletedCallback(callback);
7520}
7521
7522
7523void Isolate::RemoveCallCompletedCallback(CallCompletedCallback callback) {
7524 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
7525 isolate->RemoveCallCompletedCallback(callback);
7526}
7527
7528
Ben Murdoch097c5b22016-05-18 11:27:45 +01007529void Isolate::AddCallCompletedCallback(
7530 DeprecatedCallCompletedCallback callback) {
7531 AddCallCompletedCallback(reinterpret_cast<CallCompletedCallback>(callback));
7532}
7533
7534
7535void Isolate::RemoveCallCompletedCallback(
7536 DeprecatedCallCompletedCallback callback) {
7537 RemoveCallCompletedCallback(
7538 reinterpret_cast<CallCompletedCallback>(callback));
7539}
7540
7541
Emily Bernierd0a1eb72015-03-24 16:35:39 -04007542void Isolate::SetPromiseRejectCallback(PromiseRejectCallback callback) {
7543 if (callback == NULL) return;
7544 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
7545 isolate->SetPromiseRejectCallback(callback);
7546}
7547
7548
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007549void Isolate::RunMicrotasks() {
Ben Murdochda12d292016-06-02 14:46:10 +01007550 DCHECK(MicrotasksPolicy::kScoped != GetMicrotasksPolicy());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007551 reinterpret_cast<i::Isolate*>(this)->RunMicrotasks();
7552}
7553
7554
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007555void Isolate::EnqueueMicrotask(Local<Function> microtask) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007556 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
7557 isolate->EnqueueMicrotask(Utils::OpenHandle(*microtask));
7558}
7559
7560
7561void Isolate::EnqueueMicrotask(MicrotaskCallback microtask, void* data) {
7562 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
7563 i::HandleScope scope(isolate);
7564 i::Handle<i::CallHandlerInfo> callback_info =
7565 i::Handle<i::CallHandlerInfo>::cast(
7566 isolate->factory()->NewStruct(i::CALL_HANDLER_INFO_TYPE));
7567 SET_FIELD_WRAPPED(callback_info, set_callback, microtask);
7568 SET_FIELD_WRAPPED(callback_info, set_data, data);
7569 isolate->EnqueueMicrotask(callback_info);
7570}
7571
7572
7573void Isolate::SetAutorunMicrotasks(bool autorun) {
Ben Murdochda12d292016-06-02 14:46:10 +01007574 SetMicrotasksPolicy(
7575 autorun ? MicrotasksPolicy::kAuto : MicrotasksPolicy::kExplicit);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007576}
7577
7578
7579bool Isolate::WillAutorunMicrotasks() const {
Ben Murdochda12d292016-06-02 14:46:10 +01007580 return GetMicrotasksPolicy() == MicrotasksPolicy::kAuto;
7581}
7582
7583
7584void Isolate::SetMicrotasksPolicy(MicrotasksPolicy policy) {
7585 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
7586 isolate->handle_scope_implementer()->set_microtasks_policy(policy);
7587}
7588
7589
7590MicrotasksPolicy Isolate::GetMicrotasksPolicy() const {
7591 i::Isolate* isolate =
7592 reinterpret_cast<i::Isolate*>(const_cast<Isolate*>(this));
7593 return isolate->handle_scope_implementer()->microtasks_policy();
7594}
7595
7596
7597void Isolate::AddMicrotasksCompletedCallback(
7598 MicrotasksCompletedCallback callback) {
7599 DCHECK(callback);
7600 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
7601 isolate->AddMicrotasksCompletedCallback(callback);
7602}
7603
7604
7605void Isolate::RemoveMicrotasksCompletedCallback(
7606 MicrotasksCompletedCallback callback) {
7607 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
7608 isolate->RemoveMicrotasksCompletedCallback(callback);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007609}
7610
7611
7612void Isolate::SetUseCounterCallback(UseCounterCallback callback) {
7613 reinterpret_cast<i::Isolate*>(this)->SetUseCounterCallback(callback);
7614}
7615
7616
7617void Isolate::SetCounterFunction(CounterLookupCallback callback) {
7618 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
7619 isolate->stats_table()->SetCounterFunction(callback);
7620 isolate->InitializeLoggingAndCounters();
7621 isolate->counters()->ResetCounters();
7622}
7623
7624
7625void Isolate::SetCreateHistogramFunction(CreateHistogramCallback callback) {
7626 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
7627 isolate->stats_table()->SetCreateHistogramFunction(callback);
7628 isolate->InitializeLoggingAndCounters();
7629 isolate->counters()->ResetHistograms();
7630}
7631
7632
7633void Isolate::SetAddHistogramSampleFunction(
7634 AddHistogramSampleCallback callback) {
7635 reinterpret_cast<i::Isolate*>(this)
7636 ->stats_table()
7637 ->SetAddHistogramSampleFunction(callback);
7638}
7639
7640
Emily Bernierd0a1eb72015-03-24 16:35:39 -04007641bool Isolate::IdleNotification(int idle_time_in_ms) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007642 // Returning true tells the caller that it need not
7643 // continue to call IdleNotification.
7644 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
7645 if (!i::FLAG_use_idle_notification) return true;
7646 return isolate->heap()->IdleNotification(idle_time_in_ms);
7647}
7648
7649
Emily Bernierd0a1eb72015-03-24 16:35:39 -04007650bool Isolate::IdleNotificationDeadline(double deadline_in_seconds) {
7651 // Returning true tells the caller that it need not
7652 // continue to call IdleNotification.
7653 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
7654 if (!i::FLAG_use_idle_notification) return true;
7655 return isolate->heap()->IdleNotification(deadline_in_seconds);
7656}
7657
7658
7659void Isolate::LowMemoryNotification() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007660 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
7661 {
7662 i::HistogramTimerScope idle_notification_scope(
7663 isolate->counters()->gc_low_memory_notification());
Ben Murdoch097c5b22016-05-18 11:27:45 +01007664 TRACE_EVENT0("v8", "V8.GCLowMemoryNotification");
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007665 isolate->heap()->CollectAllAvailableGarbage("low memory notification");
7666 }
7667}
7668
7669
Emily Bernierd0a1eb72015-03-24 16:35:39 -04007670int Isolate::ContextDisposedNotification(bool dependant_context) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007671 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04007672 return isolate->heap()->NotifyContextDisposed(dependant_context);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007673}
7674
7675
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007676void Isolate::IsolateInForegroundNotification() {
7677 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
7678 return isolate->heap()->SetOptimizeForLatency();
7679}
7680
7681
7682void Isolate::IsolateInBackgroundNotification() {
7683 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
7684 return isolate->heap()->SetOptimizeForMemoryUsage();
7685}
7686
Ben Murdochda12d292016-06-02 14:46:10 +01007687void Isolate::MemoryPressureNotification(MemoryPressureLevel level) {
7688 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
7689 return isolate->heap()->MemoryPressureNotification(level,
7690 Locker::IsLocked(this));
7691}
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007692
Emily Bernierd0a1eb72015-03-24 16:35:39 -04007693void Isolate::SetJitCodeEventHandler(JitCodeEventOptions options,
7694 JitCodeEventHandler event_handler) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007695 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
7696 // Ensure that logging is initialized for our isolate.
7697 isolate->InitializeLoggingAndCounters();
7698 isolate->logger()->SetCodeEventHandler(options, event_handler);
7699}
7700
7701
Emily Bernierd0a1eb72015-03-24 16:35:39 -04007702void Isolate::SetStackLimit(uintptr_t stack_limit) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007703 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
7704 CHECK(stack_limit);
7705 isolate->stack_guard()->SetStackLimit(stack_limit);
Ben Murdoch257744e2011-11-30 15:57:28 +00007706}
7707
7708
Emily Bernierd0a1eb72015-03-24 16:35:39 -04007709void Isolate::GetCodeRange(void** start, size_t* length_in_bytes) {
7710 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
Ben Murdochc5610432016-08-08 18:44:38 +01007711 if (isolate->heap()->memory_allocator()->code_range()->valid()) {
7712 *start = isolate->heap()->memory_allocator()->code_range()->start();
7713 *length_in_bytes =
7714 isolate->heap()->memory_allocator()->code_range()->size();
Emily Bernierd0a1eb72015-03-24 16:35:39 -04007715 } else {
7716 *start = NULL;
7717 *length_in_bytes = 0;
7718 }
7719}
7720
7721
7722void Isolate::SetFatalErrorHandler(FatalErrorCallback that) {
7723 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
7724 isolate->set_exception_behavior(that);
7725}
7726
7727
7728void Isolate::SetAllowCodeGenerationFromStringsCallback(
7729 AllowCodeGenerationFromStringsCallback callback) {
7730 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
7731 isolate->set_allow_code_gen_callback(callback);
7732}
7733
7734
7735bool Isolate::IsDead() {
7736 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
7737 return isolate->IsDead();
7738}
7739
7740
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007741bool Isolate::AddMessageListener(MessageCallback that, Local<Value> data) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04007742 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04007743 ENTER_V8(isolate);
7744 i::HandleScope scope(isolate);
7745 NeanderArray listeners(isolate->factory()->message_listeners());
7746 NeanderObject obj(isolate, 2);
7747 obj.set(0, *isolate->factory()->NewForeign(FUNCTION_ADDR(that)));
7748 obj.set(1, data.IsEmpty() ? isolate->heap()->undefined_value()
7749 : *Utils::OpenHandle(*data));
7750 listeners.add(isolate, obj.value());
7751 return true;
7752}
7753
7754
7755void Isolate::RemoveMessageListeners(MessageCallback that) {
7756 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04007757 ENTER_V8(isolate);
7758 i::HandleScope scope(isolate);
7759 NeanderArray listeners(isolate->factory()->message_listeners());
7760 for (int i = 0; i < listeners.length(); i++) {
7761 if (listeners.get(i)->IsUndefined()) continue; // skip deleted ones
7762
7763 NeanderObject listener(i::JSObject::cast(listeners.get(i)));
7764 i::Handle<i::Foreign> callback_obj(i::Foreign::cast(listener.get(0)));
7765 if (callback_obj->foreign_address() == FUNCTION_ADDR(that)) {
7766 listeners.set(i, isolate->heap()->undefined_value());
7767 }
7768 }
7769}
7770
7771
7772void Isolate::SetFailedAccessCheckCallbackFunction(
7773 FailedAccessCheckCallback callback) {
7774 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
7775 isolate->SetFailedAccessCheckCallback(callback);
7776}
7777
7778
7779void Isolate::SetCaptureStackTraceForUncaughtExceptions(
7780 bool capture, int frame_limit, StackTrace::StackTraceOptions options) {
7781 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
7782 isolate->SetCaptureStackTraceForUncaughtExceptions(capture, frame_limit,
7783 options);
7784}
7785
7786
7787void Isolate::VisitExternalResources(ExternalResourceVisitor* visitor) {
7788 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
7789 isolate->heap()->VisitExternalResources(visitor);
7790}
7791
7792
7793class VisitorAdapter : public i::ObjectVisitor {
7794 public:
7795 explicit VisitorAdapter(PersistentHandleVisitor* visitor)
7796 : visitor_(visitor) {}
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007797 void VisitPointers(i::Object** start, i::Object** end) override {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04007798 UNREACHABLE();
7799 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007800 void VisitEmbedderReference(i::Object** p, uint16_t class_id) override {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04007801 Value* value = ToApi<Value>(i::Handle<i::Object>(p));
7802 visitor_->VisitPersistentHandle(
7803 reinterpret_cast<Persistent<Value>*>(&value), class_id);
7804 }
7805
7806 private:
7807 PersistentHandleVisitor* visitor_;
7808};
7809
7810
7811void Isolate::VisitHandlesWithClassIds(PersistentHandleVisitor* visitor) {
7812 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
7813 i::DisallowHeapAllocation no_allocation;
7814 VisitorAdapter visitor_adapter(visitor);
7815 isolate->global_handles()->IterateAllRootsWithClassIds(&visitor_adapter);
7816}
7817
7818
7819void Isolate::VisitHandlesForPartialDependence(
7820 PersistentHandleVisitor* visitor) {
7821 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
7822 i::DisallowHeapAllocation no_allocation;
7823 VisitorAdapter visitor_adapter(visitor);
7824 isolate->global_handles()->IterateAllRootsInNewSpaceWithClassIds(
7825 &visitor_adapter);
7826}
7827
7828
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007829void Isolate::VisitWeakHandles(PersistentHandleVisitor* visitor) {
7830 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
7831 i::DisallowHeapAllocation no_allocation;
7832 VisitorAdapter visitor_adapter(visitor);
7833 isolate->global_handles()->IterateWeakRootsInNewSpaceWithClassIds(
7834 &visitor_adapter);
7835}
7836
7837
Ben Murdochda12d292016-06-02 14:46:10 +01007838MicrotasksScope::MicrotasksScope(Isolate* isolate, MicrotasksScope::Type type)
7839 : isolate_(reinterpret_cast<i::Isolate*>(isolate)),
7840 run_(type == MicrotasksScope::kRunMicrotasks) {
7841 auto handle_scope_implementer = isolate_->handle_scope_implementer();
7842 if (run_) handle_scope_implementer->IncrementMicrotasksScopeDepth();
7843#ifdef DEBUG
7844 if (!run_) handle_scope_implementer->IncrementDebugMicrotasksScopeDepth();
7845#endif
7846}
7847
7848
7849MicrotasksScope::~MicrotasksScope() {
7850 auto handle_scope_implementer = isolate_->handle_scope_implementer();
7851 if (run_) {
7852 handle_scope_implementer->DecrementMicrotasksScopeDepth();
7853 if (MicrotasksPolicy::kScoped ==
7854 handle_scope_implementer->microtasks_policy()) {
7855 PerformCheckpoint(reinterpret_cast<Isolate*>(isolate_));
7856 }
7857 }
7858#ifdef DEBUG
7859 if (!run_) handle_scope_implementer->DecrementDebugMicrotasksScopeDepth();
7860#endif
7861}
7862
7863
7864void MicrotasksScope::PerformCheckpoint(Isolate* v8Isolate) {
7865 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(v8Isolate);
7866 if (IsExecutionTerminatingCheck(isolate)) return;
7867 auto handle_scope_implementer = isolate->handle_scope_implementer();
7868 if (!handle_scope_implementer->GetMicrotasksScopeDepth() &&
7869 !handle_scope_implementer->HasMicrotasksSuppressions()) {
7870 isolate->RunMicrotasks();
7871 }
7872}
7873
7874
7875int MicrotasksScope::GetCurrentDepth(Isolate* v8Isolate) {
7876 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(v8Isolate);
7877 return isolate->handle_scope_implementer()->GetMicrotasksScopeDepth();
7878}
7879
Ben Murdochc5610432016-08-08 18:44:38 +01007880bool MicrotasksScope::IsRunningMicrotasks(Isolate* v8Isolate) {
7881 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(v8Isolate);
7882 return isolate->IsRunningMicrotasks();
7883}
Ben Murdochda12d292016-06-02 14:46:10 +01007884
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007885String::Utf8Value::Utf8Value(v8::Local<v8::Value> obj)
Ben Murdoch257744e2011-11-30 15:57:28 +00007886 : str_(NULL), length_(0) {
Ben Murdoch257744e2011-11-30 15:57:28 +00007887 if (obj.IsEmpty()) return;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007888 i::Isolate* isolate = i::Isolate::Current();
7889 Isolate* v8_isolate = reinterpret_cast<Isolate*>(isolate);
Steve Block44f0eee2011-05-26 01:26:41 +01007890 ENTER_V8(isolate);
7891 i::HandleScope scope(isolate);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007892 Local<Context> context = v8_isolate->GetCurrentContext();
7893 TryCatch try_catch(v8_isolate);
7894 Local<String> str;
7895 if (!obj->ToString(context).ToLocal(&str)) return;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01007896 i::Handle<i::String> i_str = Utils::OpenHandle(*str);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007897 length_ = v8::Utf8Length(*i_str, isolate);
Ben Murdoch257744e2011-11-30 15:57:28 +00007898 str_ = i::NewArray<char>(length_ + 1);
7899 str->WriteUtf8(str_);
Steve Blocka7e24c12009-10-30 11:49:00 +00007900}
7901
7902
7903String::Utf8Value::~Utf8Value() {
7904 i::DeleteArray(str_);
7905}
7906
7907
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007908String::Value::Value(v8::Local<v8::Value> obj) : str_(NULL), length_(0) {
Ben Murdoch257744e2011-11-30 15:57:28 +00007909 if (obj.IsEmpty()) return;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007910 i::Isolate* isolate = i::Isolate::Current();
7911 Isolate* v8_isolate = reinterpret_cast<Isolate*>(isolate);
Steve Block44f0eee2011-05-26 01:26:41 +01007912 ENTER_V8(isolate);
7913 i::HandleScope scope(isolate);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007914 Local<Context> context = v8_isolate->GetCurrentContext();
7915 TryCatch try_catch(v8_isolate);
7916 Local<String> str;
7917 if (!obj->ToString(context).ToLocal(&str)) return;
Ben Murdoch257744e2011-11-30 15:57:28 +00007918 length_ = str->Length();
7919 str_ = i::NewArray<uint16_t>(length_ + 1);
7920 str->Write(str_);
Steve Blocka7e24c12009-10-30 11:49:00 +00007921}
7922
7923
7924String::Value::~Value() {
7925 i::DeleteArray(str_);
7926}
7927
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007928#define DEFINE_ERROR(NAME, name) \
7929 Local<Value> Exception::NAME(v8::Local<v8::String> raw_message) { \
7930 i::Isolate* isolate = i::Isolate::Current(); \
Ben Murdochc5610432016-08-08 18:44:38 +01007931 LOG_API(isolate, NAME, New); \
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007932 ENTER_V8(isolate); \
7933 i::Object* error; \
7934 { \
7935 i::HandleScope scope(isolate); \
7936 i::Handle<i::String> message = Utils::OpenHandle(*raw_message); \
7937 i::Handle<i::JSFunction> constructor = isolate->name##_function(); \
7938 error = *isolate->factory()->NewError(constructor, message); \
7939 } \
7940 i::Handle<i::Object> result(error, isolate); \
7941 return Utils::ToLocal(result); \
Steve Blocka7e24c12009-10-30 11:49:00 +00007942 }
Steve Blocka7e24c12009-10-30 11:49:00 +00007943
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007944DEFINE_ERROR(RangeError, range_error)
7945DEFINE_ERROR(ReferenceError, reference_error)
7946DEFINE_ERROR(SyntaxError, syntax_error)
7947DEFINE_ERROR(TypeError, type_error)
7948DEFINE_ERROR(Error, error)
Steve Blocka7e24c12009-10-30 11:49:00 +00007949
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007950#undef DEFINE_ERROR
Steve Blocka7e24c12009-10-30 11:49:00 +00007951
7952
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007953Local<Message> Exception::CreateMessage(Isolate* isolate,
7954 Local<Value> exception) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04007955 i::Handle<i::Object> obj = Utils::OpenHandle(*exception);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007956 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
7957 ENTER_V8(i_isolate);
7958 i::HandleScope scope(i_isolate);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04007959 return Utils::MessageToLocal(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007960 scope.CloseAndEscape(i_isolate->CreateMessage(obj, NULL)));
Emily Bernierd0a1eb72015-03-24 16:35:39 -04007961}
7962
7963
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007964Local<Message> Exception::CreateMessage(Local<Value> exception) {
7965 i::Handle<i::Object> obj = Utils::OpenHandle(*exception);
7966 if (!obj->IsHeapObject()) return Local<Message>();
7967 i::Isolate* isolate = i::HeapObject::cast(*obj)->GetIsolate();
7968 return CreateMessage(reinterpret_cast<Isolate*>(isolate), exception);
7969}
7970
7971
7972Local<StackTrace> Exception::GetStackTrace(Local<Value> exception) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04007973 i::Handle<i::Object> obj = Utils::OpenHandle(*exception);
7974 if (!obj->IsJSObject()) return Local<StackTrace>();
7975 i::Handle<i::JSObject> js_obj = i::Handle<i::JSObject>::cast(obj);
7976 i::Isolate* isolate = js_obj->GetIsolate();
7977 ENTER_V8(isolate);
7978 return Utils::StackTraceToLocal(isolate->GetDetailedStackTrace(js_obj));
7979}
7980
7981
Steve Blocka7e24c12009-10-30 11:49:00 +00007982// --- D e b u g S u p p o r t ---
7983
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007984bool Debug::SetDebugEventListener(Isolate* isolate, EventCallback that,
7985 Local<Value> data) {
7986 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
7987 ENTER_V8(i_isolate);
7988 i::HandleScope scope(i_isolate);
7989 i::Handle<i::Object> foreign = i_isolate->factory()->undefined_value();
Steve Blocka7e24c12009-10-30 11:49:00 +00007990 if (that != NULL) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007991 foreign = i_isolate->factory()->NewForeign(FUNCTION_ADDR(that));
Steve Blocka7e24c12009-10-30 11:49:00 +00007992 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007993 i_isolate->debug()->SetEventListener(foreign, Utils::OpenHandle(*data, true));
Steve Blocka7e24c12009-10-30 11:49:00 +00007994 return true;
7995}
7996
7997
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007998bool Debug::SetDebugEventListener(EventCallback that, Local<Value> data) {
7999 return SetDebugEventListener(
8000 reinterpret_cast<Isolate*>(i::Isolate::Current()), that, data);
8001}
8002
8003
Steve Block44f0eee2011-05-26 01:26:41 +01008004void Debug::DebugBreak(Isolate* isolate) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008005 reinterpret_cast<i::Isolate*>(isolate)->stack_guard()->RequestDebugBreak();
Steve Blocka7e24c12009-10-30 11:49:00 +00008006}
8007
8008
Steve Block44f0eee2011-05-26 01:26:41 +01008009void Debug::CancelDebugBreak(Isolate* isolate) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008010 i::Isolate* internal_isolate = reinterpret_cast<i::Isolate*>(isolate);
8011 internal_isolate->stack_guard()->ClearDebugBreak();
Ben Murdochf87a2032010-10-22 12:50:53 +01008012}
8013
8014
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008015bool Debug::CheckDebugBreak(Isolate* isolate) {
8016 i::Isolate* internal_isolate = reinterpret_cast<i::Isolate*>(isolate);
8017 return internal_isolate->stack_guard()->CheckDebugBreak();
Ben Murdoch3bec4d22010-07-22 14:51:16 +01008018}
8019
8020
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008021void Debug::SetMessageHandler(Isolate* isolate,
8022 v8::Debug::MessageHandler handler) {
8023 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
8024 ENTER_V8(i_isolate);
8025 i_isolate->debug()->SetMessageHandler(handler);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008026}
8027
8028
8029void Debug::SetMessageHandler(v8::Debug::MessageHandler handler) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008030 SetMessageHandler(reinterpret_cast<Isolate*>(i::Isolate::Current()), handler);
Steve Blocka7e24c12009-10-30 11:49:00 +00008031}
8032
8033
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008034void Debug::SendCommand(Isolate* isolate,
8035 const uint16_t* command,
8036 int length,
8037 ClientData* client_data) {
8038 i::Isolate* internal_isolate = reinterpret_cast<i::Isolate*>(isolate);
8039 internal_isolate->debug()->EnqueueCommandMessage(
8040 i::Vector<const uint16_t>(command, length), client_data);
Steve Blockd0582a62009-12-15 09:54:21 +00008041}
8042
8043
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008044MaybeLocal<Value> Debug::Call(Local<Context> context,
8045 v8::Local<v8::Function> fun,
8046 v8::Local<v8::Value> data) {
Ben Murdochc5610432016-08-08 18:44:38 +01008047 PREPARE_FOR_EXECUTION(context, Debug, Call, Value);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008048 i::Handle<i::Object> data_obj;
Steve Blocka7e24c12009-10-30 11:49:00 +00008049 if (data.IsEmpty()) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008050 data_obj = isolate->factory()->undefined_value();
Steve Blocka7e24c12009-10-30 11:49:00 +00008051 } else {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008052 data_obj = Utils::OpenHandle(*data);
Steve Blocka7e24c12009-10-30 11:49:00 +00008053 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008054 Local<Value> result;
8055 has_pending_exception =
8056 !ToLocal<Value>(isolate->debug()->Call(Utils::OpenHandle(*fun), data_obj),
8057 &result);
8058 RETURN_ON_FAILED_EXECUTION(Value);
8059 RETURN_ESCAPED(result);
Steve Blocka7e24c12009-10-30 11:49:00 +00008060}
8061
8062
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008063Local<Value> Debug::Call(v8::Local<v8::Function> fun,
8064 v8::Local<v8::Value> data) {
8065 auto context = ContextFromHeapObject(Utils::OpenHandle(*fun));
8066 RETURN_TO_LOCAL_UNCHECKED(Call(context, fun, data), Value);
8067}
8068
8069
8070MaybeLocal<Value> Debug::GetMirror(Local<Context> context,
8071 v8::Local<v8::Value> obj) {
Ben Murdochc5610432016-08-08 18:44:38 +01008072 PREPARE_FOR_EXECUTION(context, Debug, GetMirror, Value);
Steve Block44f0eee2011-05-26 01:26:41 +01008073 i::Debug* isolate_debug = isolate->debug();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008074 has_pending_exception = !isolate_debug->Load();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008075 RETURN_ON_FAILED_EXECUTION(Value);
8076 i::Handle<i::JSObject> debug(isolate_debug->debug_context()->global_object());
8077 auto name = isolate->factory()->NewStringFromStaticChars("MakeMirror");
Ben Murdochda12d292016-06-02 14:46:10 +01008078 auto fun_obj = i::JSReceiver::GetProperty(debug, name).ToHandleChecked();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008079 auto v8_fun = Utils::CallableToLocal(i::Handle<i::JSFunction>::cast(fun_obj));
8080 const int kArgc = 1;
8081 v8::Local<v8::Value> argv[kArgc] = {obj};
8082 Local<Value> result;
8083 has_pending_exception =
8084 !v8_fun->Call(context, Utils::ToLocal(debug), kArgc, argv)
8085 .ToLocal(&result);
8086 RETURN_ON_FAILED_EXECUTION(Value);
8087 RETURN_ESCAPED(result);
8088}
8089
8090
8091Local<Value> Debug::GetMirror(v8::Local<v8::Value> obj) {
8092 RETURN_TO_LOCAL_UNCHECKED(GetMirror(Local<Context>(), obj), Value);
8093}
8094
8095
8096void Debug::ProcessDebugMessages(Isolate* isolate) {
8097 reinterpret_cast<i::Isolate*>(isolate)->debug()->ProcessDebugMessages(true);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01008098}
8099
8100
Leon Clarkee46be812010-01-19 14:06:41 +00008101void Debug::ProcessDebugMessages() {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008102 ProcessDebugMessages(reinterpret_cast<Isolate*>(i::Isolate::Current()));
8103}
8104
8105
8106Local<Context> Debug::GetDebugContext(Isolate* isolate) {
8107 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
8108 ENTER_V8(i_isolate);
8109 return Utils::ToLocal(i_isolate->debug()->GetDebugContext());
Leon Clarkee46be812010-01-19 14:06:41 +00008110}
8111
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008112
Steve Block6ded16b2010-05-10 14:33:55 +01008113Local<Context> Debug::GetDebugContext() {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008114 return GetDebugContext(reinterpret_cast<Isolate*>(i::Isolate::Current()));
Steve Block6ded16b2010-05-10 14:33:55 +01008115}
8116
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008117
8118void Debug::SetLiveEditEnabled(Isolate* isolate, bool enable) {
8119 i::Isolate* internal_isolate = reinterpret_cast<i::Isolate*>(isolate);
8120 internal_isolate->debug()->set_live_edit_enabled(enable);
8121}
Steve Blocka7e24c12009-10-30 11:49:00 +00008122
Ben Murdochda12d292016-06-02 14:46:10 +01008123bool Debug::IsTailCallEliminationEnabled(Isolate* isolate) {
8124 i::Isolate* internal_isolate = reinterpret_cast<i::Isolate*>(isolate);
8125 return internal_isolate->is_tail_call_elimination_enabled();
8126}
8127
8128void Debug::SetTailCallEliminationEnabled(Isolate* isolate, bool enabled) {
8129 i::Isolate* internal_isolate = reinterpret_cast<i::Isolate*>(isolate);
8130 internal_isolate->SetTailCallEliminationEnabled(enabled);
8131}
Steve Block6ded16b2010-05-10 14:33:55 +01008132
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008133MaybeLocal<Array> Debug::GetInternalProperties(Isolate* v8_isolate,
8134 Local<Value> value) {
8135 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(v8_isolate);
8136 ENTER_V8(isolate);
8137 i::Handle<i::Object> val = Utils::OpenHandle(*value);
8138 i::Handle<i::JSArray> result;
8139 if (!i::Runtime::GetInternalProperties(isolate, val).ToHandle(&result))
8140 return MaybeLocal<Array>();
8141 return Utils::ToLocal(result);
8142}
8143
8144
8145Local<String> CpuProfileNode::GetFunctionName() const {
Steve Block6ded16b2010-05-10 14:33:55 +01008146 const i::ProfileNode* node = reinterpret_cast<const i::ProfileNode*>(this);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008147 i::Isolate* isolate = node->isolate();
Steve Block6ded16b2010-05-10 14:33:55 +01008148 const i::CodeEntry* entry = node->entry();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008149 i::Handle<i::String> name =
8150 isolate->factory()->InternalizeUtf8String(entry->name());
Steve Block6ded16b2010-05-10 14:33:55 +01008151 if (!entry->has_name_prefix()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008152 return ToApiHandle<String>(name);
Steve Block6ded16b2010-05-10 14:33:55 +01008153 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008154 // We do not expect this to fail. Change this if it does.
8155 i::Handle<i::String> cons = isolate->factory()->NewConsString(
8156 isolate->factory()->InternalizeUtf8String(entry->name_prefix()),
8157 name).ToHandleChecked();
8158 return ToApiHandle<String>(cons);
Steve Block6ded16b2010-05-10 14:33:55 +01008159 }
8160}
8161
8162
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008163int CpuProfileNode::GetScriptId() const {
8164 const i::ProfileNode* node = reinterpret_cast<const i::ProfileNode*>(this);
8165 const i::CodeEntry* entry = node->entry();
8166 return entry->script_id();
8167}
8168
8169
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008170Local<String> CpuProfileNode::GetScriptResourceName() const {
Steve Block6ded16b2010-05-10 14:33:55 +01008171 const i::ProfileNode* node = reinterpret_cast<const i::ProfileNode*>(this);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008172 i::Isolate* isolate = node->isolate();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008173 return ToApiHandle<String>(isolate->factory()->InternalizeUtf8String(
8174 node->entry()->resource_name()));
Steve Block6ded16b2010-05-10 14:33:55 +01008175}
8176
8177
8178int CpuProfileNode::GetLineNumber() const {
Steve Block6ded16b2010-05-10 14:33:55 +01008179 return reinterpret_cast<const i::ProfileNode*>(this)->entry()->line_number();
8180}
8181
8182
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008183int CpuProfileNode::GetColumnNumber() const {
8184 return reinterpret_cast<const i::ProfileNode*>(this)->
8185 entry()->column_number();
Steve Block6ded16b2010-05-10 14:33:55 +01008186}
8187
8188
Emily Bernierd0a1eb72015-03-24 16:35:39 -04008189unsigned int CpuProfileNode::GetHitLineCount() const {
8190 const i::ProfileNode* node = reinterpret_cast<const i::ProfileNode*>(this);
8191 return node->GetHitLineCount();
8192}
8193
8194
8195bool CpuProfileNode::GetLineTicks(LineTick* entries,
8196 unsigned int length) const {
8197 const i::ProfileNode* node = reinterpret_cast<const i::ProfileNode*>(this);
8198 return node->GetLineTicks(entries, length);
8199}
8200
8201
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008202const char* CpuProfileNode::GetBailoutReason() const {
8203 const i::ProfileNode* node = reinterpret_cast<const i::ProfileNode*>(this);
8204 return node->entry()->bailout_reason();
Steve Block6ded16b2010-05-10 14:33:55 +01008205}
8206
8207
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008208unsigned CpuProfileNode::GetHitCount() const {
Steve Block6ded16b2010-05-10 14:33:55 +01008209 return reinterpret_cast<const i::ProfileNode*>(this)->self_ticks();
8210}
8211
8212
8213unsigned CpuProfileNode::GetCallUid() const {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008214 return reinterpret_cast<const i::ProfileNode*>(this)->function_id();
Steve Block6ded16b2010-05-10 14:33:55 +01008215}
8216
8217
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008218unsigned CpuProfileNode::GetNodeId() const {
8219 return reinterpret_cast<const i::ProfileNode*>(this)->id();
8220}
8221
8222
Steve Block6ded16b2010-05-10 14:33:55 +01008223int CpuProfileNode::GetChildrenCount() const {
Steve Block6ded16b2010-05-10 14:33:55 +01008224 return reinterpret_cast<const i::ProfileNode*>(this)->children()->length();
8225}
8226
8227
8228const CpuProfileNode* CpuProfileNode::GetChild(int index) const {
Steve Block6ded16b2010-05-10 14:33:55 +01008229 const i::ProfileNode* child =
8230 reinterpret_cast<const i::ProfileNode*>(this)->children()->at(index);
8231 return reinterpret_cast<const CpuProfileNode*>(child);
8232}
8233
8234
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008235const std::vector<CpuProfileDeoptInfo>& CpuProfileNode::GetDeoptInfos() const {
8236 const i::ProfileNode* node = reinterpret_cast<const i::ProfileNode*>(this);
8237 return node->deopt_infos();
Steve Block6ded16b2010-05-10 14:33:55 +01008238}
8239
8240
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008241void CpuProfile::Delete() {
8242 i::CpuProfile* profile = reinterpret_cast<i::CpuProfile*>(this);
8243 i::Isolate* isolate = profile->top_down()->isolate();
8244 i::CpuProfiler* profiler = isolate->cpu_profiler();
8245 DCHECK(profiler != NULL);
8246 profiler->DeleteProfile(profile);
8247}
8248
8249
8250Local<String> CpuProfile::GetTitle() const {
Steve Block6ded16b2010-05-10 14:33:55 +01008251 const i::CpuProfile* profile = reinterpret_cast<const i::CpuProfile*>(this);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008252 i::Isolate* isolate = profile->top_down()->isolate();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008253 return ToApiHandle<String>(isolate->factory()->InternalizeUtf8String(
8254 profile->title()));
Steve Block6ded16b2010-05-10 14:33:55 +01008255}
8256
8257
8258const CpuProfileNode* CpuProfile::GetTopDownRoot() const {
Steve Block6ded16b2010-05-10 14:33:55 +01008259 const i::CpuProfile* profile = reinterpret_cast<const i::CpuProfile*>(this);
8260 return reinterpret_cast<const CpuProfileNode*>(profile->top_down()->root());
8261}
8262
8263
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008264const CpuProfileNode* CpuProfile::GetSample(int index) const {
8265 const i::CpuProfile* profile = reinterpret_cast<const i::CpuProfile*>(this);
8266 return reinterpret_cast<const CpuProfileNode*>(profile->sample(index));
Steve Block6ded16b2010-05-10 14:33:55 +01008267}
8268
8269
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008270int64_t CpuProfile::GetSampleTimestamp(int index) const {
8271 const i::CpuProfile* profile = reinterpret_cast<const i::CpuProfile*>(this);
8272 return (profile->sample_timestamp(index) - base::TimeTicks())
8273 .InMicroseconds();
Steve Block6ded16b2010-05-10 14:33:55 +01008274}
8275
8276
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008277int64_t CpuProfile::GetStartTime() const {
8278 const i::CpuProfile* profile = reinterpret_cast<const i::CpuProfile*>(this);
8279 return (profile->start_time() - base::TimeTicks()).InMicroseconds();
Steve Block6ded16b2010-05-10 14:33:55 +01008280}
8281
8282
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008283int64_t CpuProfile::GetEndTime() const {
8284 const i::CpuProfile* profile = reinterpret_cast<const i::CpuProfile*>(this);
8285 return (profile->end_time() - base::TimeTicks()).InMicroseconds();
Steve Block6ded16b2010-05-10 14:33:55 +01008286}
8287
8288
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008289int CpuProfile::GetSamplesCount() const {
8290 return reinterpret_cast<const i::CpuProfile*>(this)->samples_count();
8291}
8292
8293
8294void CpuProfiler::SetSamplingInterval(int us) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008295 DCHECK_GE(us, 0);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008296 return reinterpret_cast<i::CpuProfiler*>(this)->set_sampling_interval(
8297 base::TimeDelta::FromMicroseconds(us));
8298}
8299
Ben Murdoch097c5b22016-05-18 11:27:45 +01008300void CpuProfiler::CollectSample() {
8301 reinterpret_cast<i::CpuProfiler*>(this)->CollectSample();
8302}
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008303
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008304void CpuProfiler::StartProfiling(Local<String> title, bool record_samples) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008305 reinterpret_cast<i::CpuProfiler*>(this)->StartProfiling(
8306 *Utils::OpenHandle(*title), record_samples);
8307}
8308
8309
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008310CpuProfile* CpuProfiler::StopProfiling(Local<String> title) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008311 return reinterpret_cast<CpuProfile*>(
8312 reinterpret_cast<i::CpuProfiler*>(this)->StopProfiling(
Leon Clarkef7060e22010-06-03 12:02:55 +01008313 *Utils::OpenHandle(*title)));
Steve Block6ded16b2010-05-10 14:33:55 +01008314}
8315
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01008316
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008317void CpuProfiler::SetIdle(bool is_idle) {
Ben Murdochc5610432016-08-08 18:44:38 +01008318 i::CpuProfiler* profiler = reinterpret_cast<i::CpuProfiler*>(this);
8319 if (!profiler->is_profiling()) return;
8320 i::Isolate* isolate = profiler->isolate();
Emily Bernierd0a1eb72015-03-24 16:35:39 -04008321 v8::StateTag state = isolate->current_vm_state();
8322 DCHECK(state == v8::EXTERNAL || state == v8::IDLE);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008323 if (isolate->js_entry_sp() != NULL) return;
8324 if (is_idle) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04008325 isolate->set_current_vm_state(v8::IDLE);
8326 } else if (state == v8::IDLE) {
8327 isolate->set_current_vm_state(v8::EXTERNAL);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008328 }
Steve Block44f0eee2011-05-26 01:26:41 +01008329}
8330
8331
Iain Merrick75681382010-08-19 15:07:18 +01008332static i::HeapGraphEdge* ToInternal(const HeapGraphEdge* edge) {
8333 return const_cast<i::HeapGraphEdge*>(
8334 reinterpret_cast<const i::HeapGraphEdge*>(edge));
8335}
8336
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00008337
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01008338HeapGraphEdge::Type HeapGraphEdge::GetType() const {
Iain Merrick75681382010-08-19 15:07:18 +01008339 return static_cast<HeapGraphEdge::Type>(ToInternal(this)->type());
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01008340}
8341
8342
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008343Local<Value> HeapGraphEdge::GetName() const {
Iain Merrick75681382010-08-19 15:07:18 +01008344 i::HeapGraphEdge* edge = ToInternal(this);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008345 i::Isolate* isolate = edge->isolate();
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01008346 switch (edge->type()) {
Iain Merrick75681382010-08-19 15:07:18 +01008347 case i::HeapGraphEdge::kContextVariable:
8348 case i::HeapGraphEdge::kInternal:
8349 case i::HeapGraphEdge::kProperty:
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08008350 case i::HeapGraphEdge::kShortcut:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008351 case i::HeapGraphEdge::kWeak:
8352 return ToApiHandle<String>(
8353 isolate->factory()->InternalizeUtf8String(edge->name()));
Iain Merrick75681382010-08-19 15:07:18 +01008354 case i::HeapGraphEdge::kElement:
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08008355 case i::HeapGraphEdge::kHidden:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008356 return ToApiHandle<Number>(
8357 isolate->factory()->NewNumberFromInt(edge->index()));
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01008358 default: UNREACHABLE();
8359 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008360 return v8::Undefined(reinterpret_cast<v8::Isolate*>(isolate));
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01008361}
8362
8363
8364const HeapGraphNode* HeapGraphEdge::GetFromNode() const {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008365 const i::HeapEntry* from = ToInternal(this)->from();
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01008366 return reinterpret_cast<const HeapGraphNode*>(from);
8367}
8368
8369
8370const HeapGraphNode* HeapGraphEdge::GetToNode() const {
Iain Merrick75681382010-08-19 15:07:18 +01008371 const i::HeapEntry* to = ToInternal(this)->to();
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01008372 return reinterpret_cast<const HeapGraphNode*>(to);
8373}
8374
8375
Iain Merrick75681382010-08-19 15:07:18 +01008376static i::HeapEntry* ToInternal(const HeapGraphNode* entry) {
8377 return const_cast<i::HeapEntry*>(
8378 reinterpret_cast<const i::HeapEntry*>(entry));
8379}
8380
8381
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01008382HeapGraphNode::Type HeapGraphNode::GetType() const {
Iain Merrick75681382010-08-19 15:07:18 +01008383 return static_cast<HeapGraphNode::Type>(ToInternal(this)->type());
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01008384}
8385
8386
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008387Local<String> HeapGraphNode::GetName() const {
8388 i::Isolate* isolate = ToInternal(this)->isolate();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008389 return ToApiHandle<String>(
8390 isolate->factory()->InternalizeUtf8String(ToInternal(this)->name()));
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01008391}
8392
8393
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008394SnapshotObjectId HeapGraphNode::GetId() const {
Iain Merrick75681382010-08-19 15:07:18 +01008395 return ToInternal(this)->id();
Ben Murdoch3bec4d22010-07-22 14:51:16 +01008396}
8397
8398
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008399size_t HeapGraphNode::GetShallowSize() const {
Iain Merrick75681382010-08-19 15:07:18 +01008400 return ToInternal(this)->self_size();
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01008401}
8402
8403
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01008404int HeapGraphNode::GetChildrenCount() const {
Iain Merrick75681382010-08-19 15:07:18 +01008405 return ToInternal(this)->children().length();
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01008406}
8407
8408
8409const HeapGraphEdge* HeapGraphNode::GetChild(int index) const {
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01008410 return reinterpret_cast<const HeapGraphEdge*>(
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008411 ToInternal(this)->children()[index]);
Ben Murdoch69a99ed2011-11-30 16:03:39 +00008412}
8413
8414
Iain Merrick75681382010-08-19 15:07:18 +01008415static i::HeapSnapshot* ToInternal(const HeapSnapshot* snapshot) {
8416 return const_cast<i::HeapSnapshot*>(
8417 reinterpret_cast<const i::HeapSnapshot*>(snapshot));
8418}
8419
8420
Steve Block44f0eee2011-05-26 01:26:41 +01008421void HeapSnapshot::Delete() {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008422 i::Isolate* isolate = ToInternal(this)->profiler()->isolate();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008423 if (isolate->heap_profiler()->GetSnapshotsCount() > 1) {
Steve Block44f0eee2011-05-26 01:26:41 +01008424 ToInternal(this)->Delete();
8425 } else {
8426 // If this is the last snapshot, clean up all accessory data as well.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008427 isolate->heap_profiler()->DeleteAllSnapshots();
Steve Block44f0eee2011-05-26 01:26:41 +01008428 }
8429}
8430
8431
Ben Murdoch3bec4d22010-07-22 14:51:16 +01008432const HeapGraphNode* HeapSnapshot::GetRoot() const {
Iain Merrick75681382010-08-19 15:07:18 +01008433 return reinterpret_cast<const HeapGraphNode*>(ToInternal(this)->root());
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01008434}
8435
8436
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008437const HeapGraphNode* HeapSnapshot::GetNodeById(SnapshotObjectId id) const {
Ben Murdochb0fe1622011-05-05 13:52:32 +01008438 return reinterpret_cast<const HeapGraphNode*>(
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008439 ToInternal(this)->GetEntryById(id));
Ben Murdochb0fe1622011-05-05 13:52:32 +01008440}
8441
8442
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00008443int HeapSnapshot::GetNodesCount() const {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008444 return ToInternal(this)->entries().length();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00008445}
8446
8447
8448const HeapGraphNode* HeapSnapshot::GetNode(int index) const {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00008449 return reinterpret_cast<const HeapGraphNode*>(
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008450 &ToInternal(this)->entries().at(index));
8451}
8452
8453
8454SnapshotObjectId HeapSnapshot::GetMaxSnapshotJSObjectId() const {
8455 return ToInternal(this)->max_snapshot_js_object_id();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00008456}
8457
8458
Kristian Monsen0d5e1162010-09-30 15:31:59 +01008459void HeapSnapshot::Serialize(OutputStream* stream,
8460 HeapSnapshot::SerializationFormat format) const {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008461 Utils::ApiCheck(format == kJSON,
8462 "v8::HeapSnapshot::Serialize",
8463 "Unknown serialization format");
8464 Utils::ApiCheck(stream->GetChunkSize() > 0,
8465 "v8::HeapSnapshot::Serialize",
8466 "Invalid stream chunk size");
Kristian Monsen0d5e1162010-09-30 15:31:59 +01008467 i::HeapSnapshotJSONSerializer serializer(ToInternal(this));
8468 serializer.Serialize(stream);
8469}
8470
8471
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008472// static
8473STATIC_CONST_MEMBER_DEFINITION const SnapshotObjectId
8474 HeapProfiler::kUnknownObjectId;
8475
8476
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008477int HeapProfiler::GetSnapshotCount() {
8478 return reinterpret_cast<i::HeapProfiler*>(this)->GetSnapshotsCount();
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01008479}
8480
8481
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008482const HeapSnapshot* HeapProfiler::GetHeapSnapshot(int index) {
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01008483 return reinterpret_cast<const HeapSnapshot*>(
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008484 reinterpret_cast<i::HeapProfiler*>(this)->GetSnapshot(index));
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01008485}
8486
8487
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008488SnapshotObjectId HeapProfiler::GetObjectId(Local<Value> value) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008489 i::Handle<i::Object> obj = Utils::OpenHandle(*value);
8490 return reinterpret_cast<i::HeapProfiler*>(this)->GetSnapshotObjectId(obj);
8491}
8492
8493
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008494Local<Value> HeapProfiler::FindObjectById(SnapshotObjectId id) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008495 i::Handle<i::Object> obj =
8496 reinterpret_cast<i::HeapProfiler*>(this)->FindHeapObjectById(id);
8497 if (obj.is_null()) return Local<Value>();
8498 return Utils::ToLocal(obj);
8499}
8500
8501
8502void HeapProfiler::ClearObjectIds() {
8503 reinterpret_cast<i::HeapProfiler*>(this)->ClearHeapObjectMap();
8504}
8505
8506
8507const HeapSnapshot* HeapProfiler::TakeHeapSnapshot(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008508 ActivityControl* control, ObjectNameResolver* resolver) {
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01008509 return reinterpret_cast<const HeapSnapshot*>(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008510 reinterpret_cast<i::HeapProfiler*>(this)
8511 ->TakeSnapshot(control, resolver));
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01008512}
8513
8514
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008515void HeapProfiler::StartTrackingHeapObjects(bool track_allocations) {
8516 reinterpret_cast<i::HeapProfiler*>(this)->StartHeapObjectsTracking(
8517 track_allocations);
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01008518}
8519
Steve Block44f0eee2011-05-26 01:26:41 +01008520
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008521void HeapProfiler::StopTrackingHeapObjects() {
8522 reinterpret_cast<i::HeapProfiler*>(this)->StopHeapObjectsTracking();
Steve Block44f0eee2011-05-26 01:26:41 +01008523}
8524
8525
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008526SnapshotObjectId HeapProfiler::GetHeapStats(OutputStream* stream,
8527 int64_t* timestamp_us) {
8528 i::HeapProfiler* heap_profiler = reinterpret_cast<i::HeapProfiler*>(this);
8529 return heap_profiler->PushHeapObjectsStats(stream, timestamp_us);
Steve Block44f0eee2011-05-26 01:26:41 +01008530}
8531
Ben Murdoch097c5b22016-05-18 11:27:45 +01008532bool HeapProfiler::StartSamplingHeapProfiler(uint64_t sample_interval,
Ben Murdochc5610432016-08-08 18:44:38 +01008533 int stack_depth,
8534 SamplingFlags flags) {
8535 return reinterpret_cast<i::HeapProfiler*>(this)->StartSamplingHeapProfiler(
8536 sample_interval, stack_depth, flags);
Ben Murdoch097c5b22016-05-18 11:27:45 +01008537}
8538
8539
8540void HeapProfiler::StopSamplingHeapProfiler() {
8541 reinterpret_cast<i::HeapProfiler*>(this)->StopSamplingHeapProfiler();
8542}
8543
8544
8545AllocationProfile* HeapProfiler::GetAllocationProfile() {
8546 return reinterpret_cast<i::HeapProfiler*>(this)->GetAllocationProfile();
8547}
8548
8549
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008550void HeapProfiler::DeleteAllHeapSnapshots() {
8551 reinterpret_cast<i::HeapProfiler*>(this)->DeleteAllSnapshots();
8552}
8553
8554
8555void HeapProfiler::SetWrapperClassInfoProvider(uint16_t class_id,
8556 WrapperInfoCallback callback) {
8557 reinterpret_cast<i::HeapProfiler*>(this)->DefineWrapperClass(class_id,
8558 callback);
8559}
8560
8561
8562size_t HeapProfiler::GetProfilerMemorySize() {
8563 return reinterpret_cast<i::HeapProfiler*>(this)->
8564 GetMemorySizeUsedByProfiler();
8565}
8566
8567
8568void HeapProfiler::SetRetainedObjectInfo(UniqueId id,
8569 RetainedObjectInfo* info) {
8570 reinterpret_cast<i::HeapProfiler*>(this)->SetRetainedObjectInfo(id, info);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01008571}
8572
Steve Block6ded16b2010-05-10 14:33:55 +01008573
Ben Murdochb0fe1622011-05-05 13:52:32 +01008574v8::Testing::StressType internal::Testing::stress_type_ =
8575 v8::Testing::kStressTypeOpt;
8576
8577
8578void Testing::SetStressRunType(Testing::StressType type) {
8579 internal::Testing::set_stress_type(type);
8580}
8581
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008582
Ben Murdochb0fe1622011-05-05 13:52:32 +01008583int Testing::GetStressRuns() {
8584 if (internal::FLAG_stress_runs != 0) return internal::FLAG_stress_runs;
8585#ifdef DEBUG
8586 // In debug mode the code runs much slower so stressing will only make two
8587 // runs.
8588 return 2;
8589#else
8590 return 5;
8591#endif
8592}
8593
8594
8595static void SetFlagsFromString(const char* flags) {
8596 V8::SetFlagsFromString(flags, i::StrLength(flags));
8597}
8598
8599
8600void Testing::PrepareStressRun(int run) {
8601 static const char* kLazyOptimizations =
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008602 "--prepare-always-opt "
8603 "--max-inlined-source-size=999999 "
8604 "--max-inlined-nodes=999999 "
8605 "--max-inlined-nodes-cumulative=999999 "
8606 "--noalways-opt";
Ben Murdochb0fe1622011-05-05 13:52:32 +01008607 static const char* kForcedOptimizations = "--always-opt";
8608
8609 // If deoptimization stressed turn on frequent deoptimization. If no value
8610 // is spefified through --deopt-every-n-times use a default default value.
8611 static const char* kDeoptEvery13Times = "--deopt-every-n-times=13";
8612 if (internal::Testing::stress_type() == Testing::kStressTypeDeopt &&
8613 internal::FLAG_deopt_every_n_times == 0) {
8614 SetFlagsFromString(kDeoptEvery13Times);
8615 }
8616
8617#ifdef DEBUG
8618 // As stressing in debug mode only make two runs skip the deopt stressing
8619 // here.
8620 if (run == GetStressRuns() - 1) {
8621 SetFlagsFromString(kForcedOptimizations);
8622 } else {
Ben Murdochb0fe1622011-05-05 13:52:32 +01008623 SetFlagsFromString(kLazyOptimizations);
8624 }
8625#else
8626 if (run == GetStressRuns() - 1) {
8627 SetFlagsFromString(kForcedOptimizations);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01008628 } else if (run != GetStressRuns() - 2) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01008629 SetFlagsFromString(kLazyOptimizations);
8630 }
8631#endif
8632}
8633
8634
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008635void Testing::DeoptimizeAll(Isolate* isolate) {
8636 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
8637 i::HandleScope scope(i_isolate);
8638 internal::Deoptimizer::DeoptimizeAll(i_isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00008639}
8640
8641
Steve Block44f0eee2011-05-26 01:26:41 +01008642namespace internal {
8643
8644
Steve Blocka7e24c12009-10-30 11:49:00 +00008645void HandleScopeImplementer::FreeThreadResources() {
Steve Block44f0eee2011-05-26 01:26:41 +01008646 Free();
Steve Blocka7e24c12009-10-30 11:49:00 +00008647}
8648
8649
8650char* HandleScopeImplementer::ArchiveThread(char* storage) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008651 HandleScopeData* current = isolate_->handle_scope_data();
Steve Blocka7e24c12009-10-30 11:49:00 +00008652 handle_scope_data_ = *current;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008653 MemCopy(storage, this, sizeof(*this));
Steve Blocka7e24c12009-10-30 11:49:00 +00008654
8655 ResetAfterArchive();
8656 current->Initialize();
8657
8658 return storage + ArchiveSpacePerThread();
8659}
8660
8661
8662int HandleScopeImplementer::ArchiveSpacePerThread() {
Steve Block44f0eee2011-05-26 01:26:41 +01008663 return sizeof(HandleScopeImplementer);
Steve Blocka7e24c12009-10-30 11:49:00 +00008664}
8665
8666
8667char* HandleScopeImplementer::RestoreThread(char* storage) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008668 MemCopy(this, storage, sizeof(*this));
Ben Murdoch257744e2011-11-30 15:57:28 +00008669 *isolate_->handle_scope_data() = handle_scope_data_;
Steve Blocka7e24c12009-10-30 11:49:00 +00008670 return storage + ArchiveSpacePerThread();
8671}
8672
8673
8674void HandleScopeImplementer::IterateThis(ObjectVisitor* v) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008675#ifdef DEBUG
8676 bool found_block_before_deferred = false;
8677#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00008678 // Iterate over all handles in the blocks except for the last.
8679 for (int i = blocks()->length() - 2; i >= 0; --i) {
8680 Object** block = blocks()->at(i);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008681 if (last_handle_before_deferred_block_ != NULL &&
8682 (last_handle_before_deferred_block_ <= &block[kHandleBlockSize]) &&
8683 (last_handle_before_deferred_block_ >= block)) {
8684 v->VisitPointers(block, last_handle_before_deferred_block_);
8685 DCHECK(!found_block_before_deferred);
8686#ifdef DEBUG
8687 found_block_before_deferred = true;
8688#endif
8689 } else {
8690 v->VisitPointers(block, &block[kHandleBlockSize]);
8691 }
Steve Blocka7e24c12009-10-30 11:49:00 +00008692 }
8693
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008694 DCHECK(last_handle_before_deferred_block_ == NULL ||
8695 found_block_before_deferred);
8696
Steve Blocka7e24c12009-10-30 11:49:00 +00008697 // Iterate over live handles in the last block (if any).
8698 if (!blocks()->is_empty()) {
8699 v->VisitPointers(blocks()->last(), handle_scope_data_.next);
8700 }
8701
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008702 List<Context*>* context_lists[2] = { &saved_contexts_, &entered_contexts_};
8703 for (unsigned i = 0; i < arraysize(context_lists); i++) {
8704 if (context_lists[i]->is_empty()) continue;
8705 Object** start = reinterpret_cast<Object**>(&context_lists[i]->first());
8706 v->VisitPointers(start, start + context_lists[i]->length());
Steve Blocka7e24c12009-10-30 11:49:00 +00008707 }
8708}
8709
8710
8711void HandleScopeImplementer::Iterate(ObjectVisitor* v) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008712 HandleScopeData* current = isolate_->handle_scope_data();
Steve Block44f0eee2011-05-26 01:26:41 +01008713 handle_scope_data_ = *current;
8714 IterateThis(v);
Steve Blocka7e24c12009-10-30 11:49:00 +00008715}
8716
8717
8718char* HandleScopeImplementer::Iterate(ObjectVisitor* v, char* storage) {
Steve Block44f0eee2011-05-26 01:26:41 +01008719 HandleScopeImplementer* scope_implementer =
Steve Blocka7e24c12009-10-30 11:49:00 +00008720 reinterpret_cast<HandleScopeImplementer*>(storage);
Steve Block44f0eee2011-05-26 01:26:41 +01008721 scope_implementer->IterateThis(v);
Steve Blocka7e24c12009-10-30 11:49:00 +00008722 return storage + ArchiveSpacePerThread();
8723}
8724
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008725
8726DeferredHandles* HandleScopeImplementer::Detach(Object** prev_limit) {
8727 DeferredHandles* deferred =
8728 new DeferredHandles(isolate()->handle_scope_data()->next, isolate());
8729
8730 while (!blocks_.is_empty()) {
8731 Object** block_start = blocks_.last();
8732 Object** block_limit = &block_start[kHandleBlockSize];
8733 // We should not need to check for SealHandleScope here. Assert this.
8734 DCHECK(prev_limit == block_limit ||
8735 !(block_start <= prev_limit && prev_limit <= block_limit));
8736 if (prev_limit == block_limit) break;
8737 deferred->blocks_.Add(blocks_.last());
8738 blocks_.RemoveLast();
8739 }
8740
8741 // deferred->blocks_ now contains the blocks installed on the
8742 // HandleScope stack since BeginDeferredScope was called, but in
8743 // reverse order.
8744
8745 DCHECK(prev_limit == NULL || !blocks_.is_empty());
8746
8747 DCHECK(!blocks_.is_empty() && prev_limit != NULL);
8748 DCHECK(last_handle_before_deferred_block_ != NULL);
8749 last_handle_before_deferred_block_ = NULL;
8750 return deferred;
8751}
8752
8753
8754void HandleScopeImplementer::BeginDeferredScope() {
8755 DCHECK(last_handle_before_deferred_block_ == NULL);
8756 last_handle_before_deferred_block_ = isolate()->handle_scope_data()->next;
8757}
8758
8759
8760DeferredHandles::~DeferredHandles() {
8761 isolate_->UnlinkDeferredHandles(this);
8762
8763 for (int i = 0; i < blocks_.length(); i++) {
8764#ifdef ENABLE_HANDLE_ZAPPING
8765 HandleScope::ZapRange(blocks_[i], &blocks_[i][kHandleBlockSize]);
8766#endif
8767 isolate_->handle_scope_implementer()->ReturnBlock(blocks_[i]);
8768 }
8769}
8770
8771
8772void DeferredHandles::Iterate(ObjectVisitor* v) {
8773 DCHECK(!blocks_.is_empty());
8774
8775 DCHECK((first_block_limit_ >= blocks_.first()) &&
8776 (first_block_limit_ <= &(blocks_.first())[kHandleBlockSize]));
8777
8778 v->VisitPointers(blocks_.first(), first_block_limit_);
8779
8780 for (int i = 1; i < blocks_.length(); i++) {
8781 v->VisitPointers(blocks_[i], &blocks_[i][kHandleBlockSize]);
8782 }
8783}
8784
8785
8786void InvokeAccessorGetterCallback(
8787 v8::Local<v8::Name> property,
8788 const v8::PropertyCallbackInfo<v8::Value>& info,
8789 v8::AccessorNameGetterCallback getter) {
8790 // Leaving JavaScript.
8791 Isolate* isolate = reinterpret_cast<Isolate*>(info.GetIsolate());
Ben Murdochc5610432016-08-08 18:44:38 +01008792 RuntimeCallTimerScope timer(isolate,
8793 &RuntimeCallStats::AccessorGetterCallback);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008794 Address getter_address = reinterpret_cast<Address>(reinterpret_cast<intptr_t>(
8795 getter));
8796 VMState<EXTERNAL> state(isolate);
8797 ExternalCallbackScope call_scope(isolate, getter_address);
8798 getter(property, info);
8799}
8800
8801
8802void InvokeFunctionCallback(const v8::FunctionCallbackInfo<v8::Value>& info,
8803 v8::FunctionCallback callback) {
8804 Isolate* isolate = reinterpret_cast<Isolate*>(info.GetIsolate());
Ben Murdochc5610432016-08-08 18:44:38 +01008805 RuntimeCallTimerScope timer(isolate,
8806 &RuntimeCallStats::InvokeFunctionCallback);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008807 Address callback_address =
8808 reinterpret_cast<Address>(reinterpret_cast<intptr_t>(callback));
8809 VMState<EXTERNAL> state(isolate);
8810 ExternalCallbackScope call_scope(isolate, callback_address);
8811 callback(info);
8812}
8813
8814
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008815} // namespace internal
8816} // namespace v8