Line data Source code
1 : // Copyright 2015 the V8 project authors. All rights reserved.
2 : // Use of this source code is governed by a BSD-style license that can be
3 : // found in the LICENSE file.
4 :
5 : #include "src/wasm/wasm-result.h"
6 :
7 : #include "src/factory.h"
8 : #include "src/heap/heap.h"
9 : #include "src/isolate-inl.h"
10 : #include "src/objects.h"
11 :
12 : #include "src/base/platform/platform.h"
13 :
14 : namespace v8 {
15 : namespace internal {
16 : namespace wasm {
17 :
18 16548 : void ErrorThrower::Format(i::Handle<i::JSFunction> constructor,
19 : const char* format, va_list args) {
20 : // Only report the first error.
21 16548 : if (error()) return;
22 :
23 : constexpr int kMaxErrorMessageLength = 256;
24 : EmbeddedVector<char, kMaxErrorMessageLength> buffer;
25 :
26 : int context_len = 0;
27 16548 : if (context_) {
28 16548 : context_len = SNPrintF(buffer, "%s: ", context_);
29 16548 : CHECK_LE(0, context_len); // check for overflow.
30 : }
31 :
32 : int message_len =
33 33096 : VSNPrintF(buffer.SubVector(context_len, buffer.length()), format, args);
34 16548 : CHECK_LE(0, message_len); // check for overflow.
35 :
36 16548 : Vector<char> whole_message = buffer.SubVector(0, context_len + message_len);
37 : i::Handle<i::String> message =
38 : isolate_->factory()
39 : ->NewStringFromOneByte(Vector<uint8_t>::cast(whole_message))
40 33096 : .ToHandleChecked();
41 16548 : exception_ = isolate_->factory()->NewError(constructor, message);
42 : }
43 :
44 13121 : void ErrorThrower::TypeError(const char* format, ...) {
45 13121 : if (error()) return;
46 : va_list arguments;
47 13121 : va_start(arguments, format);
48 13121 : Format(isolate_->type_error_function(), format, arguments);
49 13121 : va_end(arguments);
50 : }
51 :
52 1335 : void ErrorThrower::RangeError(const char* format, ...) {
53 1335 : if (error()) return;
54 : va_list arguments;
55 1335 : va_start(arguments, format);
56 1335 : Format(isolate_->range_error_function(), format, arguments);
57 1335 : va_end(arguments);
58 : }
59 :
60 2493 : void ErrorThrower::CompileError(const char* format, ...) {
61 3432 : if (error()) return;
62 1554 : wasm_error_ = true;
63 : va_list arguments;
64 1554 : va_start(arguments, format);
65 1554 : Format(isolate_->wasm_compile_error_function(), format, arguments);
66 1554 : va_end(arguments);
67 : }
68 :
69 452 : void ErrorThrower::LinkError(const char* format, ...) {
70 452 : if (error()) return;
71 452 : wasm_error_ = true;
72 : va_list arguments;
73 452 : va_start(arguments, format);
74 452 : Format(isolate_->wasm_link_error_function(), format, arguments);
75 452 : va_end(arguments);
76 : }
77 :
78 86 : void ErrorThrower::RuntimeError(const char* format, ...) {
79 86 : if (error()) return;
80 86 : wasm_error_ = true;
81 : va_list arguments;
82 86 : va_start(arguments, format);
83 86 : Format(isolate_->wasm_runtime_error_function(), format, arguments);
84 86 : va_end(arguments);
85 : }
86 :
87 159781 : ErrorThrower::~ErrorThrower() {
88 159781 : if (error() && !isolate_->has_pending_exception()) {
89 5645 : isolate_->ScheduleThrow(*exception_);
90 : }
91 159781 : }
92 : } // namespace wasm
93 : } // namespace internal
94 : } // namespace v8
|