OLD | NEW |
---|---|
1 // Copyright 2015 the V8 project authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "src/asmjs/asm-js.h" | 5 #include "src/asmjs/asm-js.h" |
6 | 6 |
7 #include "src/api-natives.h" | 7 #include "src/api-natives.h" |
8 #include "src/api.h" | 8 #include "src/api.h" |
9 #include "src/asmjs/asm-typer.h" | 9 #include "src/asmjs/asm-typer.h" |
10 #include "src/asmjs/asm-wasm-builder.h" | 10 #include "src/asmjs/asm-wasm-builder.h" |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
55 | 55 |
56 bool IsStdlibMemberValid(i::Isolate* isolate, Handle<JSReceiver> stdlib, | 56 bool IsStdlibMemberValid(i::Isolate* isolate, Handle<JSReceiver> stdlib, |
57 i::Handle<i::Object> member_id) { | 57 i::Handle<i::Object> member_id) { |
58 int32_t member_kind; | 58 int32_t member_kind; |
59 if (!member_id->ToInt32(&member_kind)) { | 59 if (!member_id->ToInt32(&member_kind)) { |
60 UNREACHABLE(); | 60 UNREACHABLE(); |
61 } | 61 } |
62 switch (member_kind) { | 62 switch (member_kind) { |
63 case wasm::AsmTyper::StandardMember::kNone: | 63 case wasm::AsmTyper::StandardMember::kNone: |
64 case wasm::AsmTyper::StandardMember::kModule: | 64 case wasm::AsmTyper::StandardMember::kModule: |
65 case wasm::AsmTyper::StandardMember::kStdlib: { | 65 case wasm::AsmTyper::StandardMember::kStdlib: |
66 case wasm::AsmTyper::StandardMember::kHeap: | |
67 case wasm::AsmTyper::StandardMember::kFFI: { | |
66 // Nothing to check for these. | 68 // Nothing to check for these. |
67 return true; | 69 return true; |
68 } | 70 } |
71 case wasm::AsmTyper::StandardMember::kInfinity: { | |
72 i::Handle<i::Name> name(isolate->factory()->InternalizeOneByteString( | |
73 STATIC_CHAR_VECTOR("Infinity"))); | |
74 i::MaybeHandle<i::Object> maybe_value = | |
75 i::Object::GetProperty(stdlib, name); | |
76 if (maybe_value.is_null()) { | |
77 return false; | |
78 } | |
79 i::Handle<i::Object> value = maybe_value.ToHandleChecked(); | |
80 if (!value->IsNumber()) { | |
81 return false; | |
82 } | |
83 if (!std::isinf(value->Number())) { | |
84 return false; | |
85 } | |
86 return true; | |
87 } | |
69 case wasm::AsmTyper::StandardMember::kNaN: { | 88 case wasm::AsmTyper::StandardMember::kNaN: { |
70 i::Handle<i::Name> name(isolate->factory()->InternalizeOneByteString( | 89 i::Handle<i::Name> name(isolate->factory()->InternalizeOneByteString( |
71 STATIC_CHAR_VECTOR("NaN"))); | 90 STATIC_CHAR_VECTOR("NaN"))); |
72 i::MaybeHandle<i::Object> maybe_value = | 91 i::MaybeHandle<i::Object> maybe_value = |
73 i::Object::GetProperty(stdlib, name); | 92 i::Object::GetProperty(stdlib, name); |
74 if (maybe_value.is_null()) { | 93 if (maybe_value.is_null()) { |
75 return false; | 94 return false; |
76 } | 95 } |
77 i::Handle<i::Object> value = maybe_value.ToHandleChecked(); | 96 i::Handle<i::Object> value = maybe_value.ToHandleChecked(); |
78 if (!value->IsNaN()) { | 97 if (!value->IsNaN()) { |
79 return false; | 98 return false; |
80 } | 99 } |
81 return true; | 100 return true; |
82 } | 101 } |
83 case wasm::AsmTyper::StandardMember::kHeap: | 102 #define STDLIB_MATH_FUNC(CamelName, fname) \ |
titzer
2016/08/22 08:41:22
That's a pretty big macro. Can you factor some of
bradn
2016/08/22 16:53:40
Good idea.
Done.
| |
84 case wasm::AsmTyper::StandardMember::kFFI: | 103 case wasm::AsmTyper::StandardMember::k##CamelName: { \ |
85 case wasm::AsmTyper::StandardMember::kInfinity: | 104 i::Handle<i::Name> math_name(isolate->factory()->InternalizeOneByteString( \ |
86 case wasm::AsmTyper::StandardMember::kMathAcos: | 105 STATIC_CHAR_VECTOR("Math"))); \ |
87 case wasm::AsmTyper::StandardMember::kMathAsin: | 106 i::MaybeHandle<i::Object> maybe_math = \ |
88 case wasm::AsmTyper::StandardMember::kMathAtan: | 107 i::Object::GetProperty(stdlib, math_name); \ |
89 case wasm::AsmTyper::StandardMember::kMathCos: | 108 if (maybe_math.is_null()) { \ |
90 case wasm::AsmTyper::StandardMember::kMathSin: | 109 return false; \ |
91 case wasm::AsmTyper::StandardMember::kMathTan: | 110 } \ |
92 case wasm::AsmTyper::StandardMember::kMathExp: | 111 i::Handle<i::Object> math = maybe_math.ToHandleChecked(); \ |
93 case wasm::AsmTyper::StandardMember::kMathLog: | 112 if (!math->IsJSReceiver()) { \ |
94 case wasm::AsmTyper::StandardMember::kMathCeil: | 113 return false; \ |
95 case wasm::AsmTyper::StandardMember::kMathFloor: | 114 } \ |
96 case wasm::AsmTyper::StandardMember::kMathSqrt: | 115 i::Handle<i::Name> name(isolate->factory()->InternalizeOneByteString( \ |
97 case wasm::AsmTyper::StandardMember::kMathAbs: | 116 STATIC_CHAR_VECTOR(#fname))); \ |
98 case wasm::AsmTyper::StandardMember::kMathClz32: | 117 i::MaybeHandle<i::Object> maybe_value = \ |
99 case wasm::AsmTyper::StandardMember::kMathMin: | 118 i::Object::GetProperty(math, name); \ |
100 case wasm::AsmTyper::StandardMember::kMathMax: | 119 if (maybe_value.is_null()) { \ |
101 case wasm::AsmTyper::StandardMember::kMathAtan2: | 120 return false; \ |
102 case wasm::AsmTyper::StandardMember::kMathPow: | 121 } \ |
103 case wasm::AsmTyper::StandardMember::kMathImul: | 122 i::Handle<i::Object> value = maybe_value.ToHandleChecked(); \ |
104 case wasm::AsmTyper::StandardMember::kMathFround: | 123 if (!value->IsJSFunction()) { \ |
105 case wasm::AsmTyper::StandardMember::kMathE: | 124 return false; \ |
106 case wasm::AsmTyper::StandardMember::kMathLN10: | 125 } \ |
107 case wasm::AsmTyper::StandardMember::kMathLN2: | 126 i::Handle<i::JSFunction> func(i::JSFunction::cast(*value)); \ |
108 case wasm::AsmTyper::StandardMember::kMathLOG2E: | 127 if (func->shared()->code() != \ |
109 case wasm::AsmTyper::StandardMember::kMathLOG10E: | 128 isolate->builtins()->builtin(Builtins::k##CamelName)) { \ |
110 case wasm::AsmTyper::StandardMember::kMathPI: | 129 return false; \ |
111 case wasm::AsmTyper::StandardMember::kMathSQRT1_2: | 130 } \ |
112 case wasm::AsmTyper::StandardMember::kMathSQRT2: | 131 return true; \ |
113 // TODO(bradnelson) Actually check these. | 132 } |
114 return true; | 133 STDLIB_MATH_FUNC(MathAcos, acos) |
134 STDLIB_MATH_FUNC(MathAsin, asin) | |
135 STDLIB_MATH_FUNC(MathAtan, atan) | |
136 STDLIB_MATH_FUNC(MathCos, cos) | |
137 STDLIB_MATH_FUNC(MathSin, sin) | |
138 STDLIB_MATH_FUNC(MathTan, tan) | |
139 STDLIB_MATH_FUNC(MathExp, exp) | |
140 STDLIB_MATH_FUNC(MathLog, log) | |
141 STDLIB_MATH_FUNC(MathCeil, ceil) | |
142 STDLIB_MATH_FUNC(MathFloor, floor) | |
143 STDLIB_MATH_FUNC(MathSqrt, sqrt) | |
144 STDLIB_MATH_FUNC(MathAbs, abs) | |
145 STDLIB_MATH_FUNC(MathClz32, clz32) | |
146 STDLIB_MATH_FUNC(MathMin, min) | |
147 STDLIB_MATH_FUNC(MathMax, max) | |
148 STDLIB_MATH_FUNC(MathAtan2, atan2) | |
149 STDLIB_MATH_FUNC(MathPow, pow) | |
150 STDLIB_MATH_FUNC(MathImul, imul) | |
151 STDLIB_MATH_FUNC(MathFround, fround) | |
152 #undef STDLIB_MATH_FUNC | |
153 #define STDLIB_MATH_CONST(cname, const_value) \ | |
154 case wasm::AsmTyper::StandardMember::kMath##cname: { \ | |
155 i::Handle<i::Name> math_name(isolate->factory()->InternalizeOneByteString( \ | |
156 STATIC_CHAR_VECTOR("Math"))); \ | |
157 i::MaybeHandle<i::Object> maybe_math = \ | |
158 i::Object::GetProperty(stdlib, math_name); \ | |
159 if (maybe_math.is_null()) { \ | |
160 return false; \ | |
161 } \ | |
162 i::Handle<i::Object> math = maybe_math.ToHandleChecked(); \ | |
163 if (!math->IsJSReceiver()) { \ | |
164 return false; \ | |
165 } \ | |
166 i::Handle<i::Name> name(isolate->factory()->InternalizeOneByteString( \ | |
167 STATIC_CHAR_VECTOR(#cname))); \ | |
168 i::MaybeHandle<i::Object> maybe_value = \ | |
169 i::Object::GetProperty(math, name); \ | |
170 if (maybe_value.is_null()) { \ | |
171 return false; \ | |
172 } \ | |
173 i::Handle<i::Object> value = maybe_value.ToHandleChecked(); \ | |
174 if (!value->IsNumber()) { \ | |
175 return false; \ | |
176 } \ | |
177 if (value->Number() != const_value) { \ | |
178 return false; \ | |
179 } \ | |
180 return true; \ | |
181 } | |
182 STDLIB_MATH_CONST(E, 2.718281828459045) | |
183 STDLIB_MATH_CONST(LN10, 2.302585092994046) | |
184 STDLIB_MATH_CONST(LN2, 0.6931471805599453) | |
185 STDLIB_MATH_CONST(LOG2E, 1.4426950408889634) | |
186 STDLIB_MATH_CONST(LOG10E, 0.4342944819032518) | |
187 STDLIB_MATH_CONST(PI, 3.141592653589793) | |
188 STDLIB_MATH_CONST(SQRT1_2, 0.7071067811865476) | |
189 STDLIB_MATH_CONST(SQRT2, 1.4142135623730951) | |
190 #undef STDLIB_MATH_CONST | |
115 default: { UNREACHABLE(); } | 191 default: { UNREACHABLE(); } |
116 } | 192 } |
117 return false; | 193 return false; |
118 } | 194 } |
119 | 195 |
120 } // namespace | 196 } // namespace |
121 | 197 |
122 MaybeHandle<FixedArray> AsmJs::ConvertAsmToWasm(ParseInfo* info) { | 198 MaybeHandle<FixedArray> AsmJs::ConvertAsmToWasm(ParseInfo* info) { |
123 ErrorThrower thrower(info->isolate(), "Asm.js -> WebAssembly conversion"); | 199 ErrorThrower thrower(info->isolate(), "Asm.js -> WebAssembly conversion"); |
124 wasm::AsmTyper typer(info->isolate(), info->zone(), *(info->script()), | 200 wasm::AsmTyper typer(info->isolate(), info->zone(), *(info->script()), |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
215 if (retval.is_null()) { | 291 if (retval.is_null()) { |
216 thrower.Error( | 292 thrower.Error( |
217 "WASM.instantiateModuleFromAsm(): foreign init function failed"); | 293 "WASM.instantiateModuleFromAsm(): foreign init function failed"); |
218 return MaybeHandle<Object>(); | 294 return MaybeHandle<Object>(); |
219 } | 295 } |
220 return maybe_module_object; | 296 return maybe_module_object; |
221 } | 297 } |
222 | 298 |
223 } // namespace internal | 299 } // namespace internal |
224 } // namespace v8 | 300 } // namespace v8 |
OLD | NEW |