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 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
46 } else if (result.failed()) { | 46 } else if (result.failed()) { |
47 thrower->Failed("", result); | 47 thrower->Failed("", result); |
48 } else { | 48 } else { |
49 compiled_module = result.val->CompileFunctions(isolate, thrower); | 49 compiled_module = result.val->CompileFunctions(isolate, thrower); |
50 } | 50 } |
51 | 51 |
52 if (result.val) delete result.val; | 52 if (result.val) delete result.val; |
53 return compiled_module; | 53 return compiled_module; |
54 } | 54 } |
55 | 55 |
| 56 Handle<i::Object> StdlibMathMember(i::Isolate* isolate, |
| 57 Handle<JSReceiver> stdlib, |
| 58 Handle<Name> name) { |
| 59 Handle<i::Name> math_name( |
| 60 isolate->factory()->InternalizeOneByteString(STATIC_CHAR_VECTOR("Math"))); |
| 61 MaybeHandle<i::Object> maybe_math = i::Object::GetProperty(stdlib, math_name); |
| 62 if (maybe_math.is_null()) { |
| 63 return Handle<i::Object>(); |
| 64 } |
| 65 Handle<i::Object> math = maybe_math.ToHandleChecked(); |
| 66 if (!math->IsJSReceiver()) { |
| 67 return Handle<i::Object>(); |
| 68 } |
| 69 MaybeHandle<i::Object> maybe_value = i::Object::GetProperty(math, name); |
| 70 if (maybe_value.is_null()) { |
| 71 return Handle<i::Object>(); |
| 72 } |
| 73 return maybe_value.ToHandleChecked(); |
| 74 } |
| 75 |
56 bool IsStdlibMemberValid(i::Isolate* isolate, Handle<JSReceiver> stdlib, | 76 bool IsStdlibMemberValid(i::Isolate* isolate, Handle<JSReceiver> stdlib, |
57 i::Handle<i::Object> member_id) { | 77 Handle<i::Object> member_id) { |
58 int32_t member_kind; | 78 int32_t member_kind; |
59 if (!member_id->ToInt32(&member_kind)) { | 79 if (!member_id->ToInt32(&member_kind)) { |
60 UNREACHABLE(); | 80 UNREACHABLE(); |
61 } | 81 } |
62 switch (member_kind) { | 82 switch (member_kind) { |
63 case wasm::AsmTyper::StandardMember::kNone: | 83 case wasm::AsmTyper::StandardMember::kNone: |
64 case wasm::AsmTyper::StandardMember::kModule: | 84 case wasm::AsmTyper::StandardMember::kModule: |
65 case wasm::AsmTyper::StandardMember::kStdlib: { | 85 case wasm::AsmTyper::StandardMember::kStdlib: |
| 86 case wasm::AsmTyper::StandardMember::kHeap: |
| 87 case wasm::AsmTyper::StandardMember::kFFI: { |
66 // Nothing to check for these. | 88 // Nothing to check for these. |
67 return true; | 89 return true; |
68 } | 90 } |
69 case wasm::AsmTyper::StandardMember::kNaN: { | 91 case wasm::AsmTyper::StandardMember::kInfinity: { |
70 i::Handle<i::Name> name(isolate->factory()->InternalizeOneByteString( | 92 Handle<i::Name> name(isolate->factory()->InternalizeOneByteString( |
71 STATIC_CHAR_VECTOR("NaN"))); | 93 STATIC_CHAR_VECTOR("Infinity"))); |
72 i::MaybeHandle<i::Object> maybe_value = | 94 MaybeHandle<i::Object> maybe_value = i::Object::GetProperty(stdlib, name); |
73 i::Object::GetProperty(stdlib, name); | |
74 if (maybe_value.is_null()) { | 95 if (maybe_value.is_null()) { |
75 return false; | 96 return false; |
76 } | 97 } |
77 i::Handle<i::Object> value = maybe_value.ToHandleChecked(); | 98 Handle<i::Object> value = maybe_value.ToHandleChecked(); |
78 if (!value->IsNaN()) { | 99 return value->IsNumber() && std::isinf(value->Number()); |
| 100 } |
| 101 case wasm::AsmTyper::StandardMember::kNaN: { |
| 102 Handle<i::Name> name(isolate->factory()->InternalizeOneByteString( |
| 103 STATIC_CHAR_VECTOR("NaN"))); |
| 104 MaybeHandle<i::Object> maybe_value = i::Object::GetProperty(stdlib, name); |
| 105 if (maybe_value.is_null()) { |
79 return false; | 106 return false; |
80 } | 107 } |
81 return true; | 108 Handle<i::Object> value = maybe_value.ToHandleChecked(); |
| 109 return value->IsNaN(); |
82 } | 110 } |
83 case wasm::AsmTyper::StandardMember::kHeap: | 111 #define STDLIB_MATH_FUNC(CamelName, fname) \ |
84 case wasm::AsmTyper::StandardMember::kFFI: | 112 case wasm::AsmTyper::StandardMember::k##CamelName: { \ |
85 case wasm::AsmTyper::StandardMember::kInfinity: | 113 Handle<i::Name> name(isolate->factory()->InternalizeOneByteString( \ |
86 case wasm::AsmTyper::StandardMember::kMathAcos: | 114 STATIC_CHAR_VECTOR(#fname))); \ |
87 case wasm::AsmTyper::StandardMember::kMathAsin: | 115 Handle<i::Object> value = StdlibMathMember(isolate, stdlib, name); \ |
88 case wasm::AsmTyper::StandardMember::kMathAtan: | 116 if (value.is_null() || !value->IsJSFunction()) { \ |
89 case wasm::AsmTyper::StandardMember::kMathCos: | 117 return false; \ |
90 case wasm::AsmTyper::StandardMember::kMathSin: | 118 } \ |
91 case wasm::AsmTyper::StandardMember::kMathTan: | 119 Handle<i::JSFunction> func(i::JSFunction::cast(*value)); \ |
92 case wasm::AsmTyper::StandardMember::kMathExp: | 120 return func->shared()->code() == \ |
93 case wasm::AsmTyper::StandardMember::kMathLog: | 121 isolate->builtins()->builtin(Builtins::k##CamelName); \ |
94 case wasm::AsmTyper::StandardMember::kMathCeil: | 122 } |
95 case wasm::AsmTyper::StandardMember::kMathFloor: | 123 STDLIB_MATH_FUNC(MathAcos, acos) |
96 case wasm::AsmTyper::StandardMember::kMathSqrt: | 124 STDLIB_MATH_FUNC(MathAsin, asin) |
97 case wasm::AsmTyper::StandardMember::kMathAbs: | 125 STDLIB_MATH_FUNC(MathAtan, atan) |
98 case wasm::AsmTyper::StandardMember::kMathClz32: | 126 STDLIB_MATH_FUNC(MathCos, cos) |
99 case wasm::AsmTyper::StandardMember::kMathMin: | 127 STDLIB_MATH_FUNC(MathSin, sin) |
100 case wasm::AsmTyper::StandardMember::kMathMax: | 128 STDLIB_MATH_FUNC(MathTan, tan) |
101 case wasm::AsmTyper::StandardMember::kMathAtan2: | 129 STDLIB_MATH_FUNC(MathExp, exp) |
102 case wasm::AsmTyper::StandardMember::kMathPow: | 130 STDLIB_MATH_FUNC(MathLog, log) |
103 case wasm::AsmTyper::StandardMember::kMathImul: | 131 STDLIB_MATH_FUNC(MathCeil, ceil) |
104 case wasm::AsmTyper::StandardMember::kMathFround: | 132 STDLIB_MATH_FUNC(MathFloor, floor) |
105 case wasm::AsmTyper::StandardMember::kMathE: | 133 STDLIB_MATH_FUNC(MathSqrt, sqrt) |
106 case wasm::AsmTyper::StandardMember::kMathLN10: | 134 STDLIB_MATH_FUNC(MathAbs, abs) |
107 case wasm::AsmTyper::StandardMember::kMathLN2: | 135 STDLIB_MATH_FUNC(MathClz32, clz32) |
108 case wasm::AsmTyper::StandardMember::kMathLOG2E: | 136 STDLIB_MATH_FUNC(MathMin, min) |
109 case wasm::AsmTyper::StandardMember::kMathLOG10E: | 137 STDLIB_MATH_FUNC(MathMax, max) |
110 case wasm::AsmTyper::StandardMember::kMathPI: | 138 STDLIB_MATH_FUNC(MathAtan2, atan2) |
111 case wasm::AsmTyper::StandardMember::kMathSQRT1_2: | 139 STDLIB_MATH_FUNC(MathPow, pow) |
112 case wasm::AsmTyper::StandardMember::kMathSQRT2: | 140 STDLIB_MATH_FUNC(MathImul, imul) |
113 // TODO(bradnelson) Actually check these. | 141 STDLIB_MATH_FUNC(MathFround, fround) |
114 return true; | 142 #undef STDLIB_MATH_FUNC |
| 143 #define STDLIB_MATH_CONST(cname, const_value) \ |
| 144 case wasm::AsmTyper::StandardMember::kMath##cname: { \ |
| 145 i::Handle<i::Name> name(isolate->factory()->InternalizeOneByteString( \ |
| 146 STATIC_CHAR_VECTOR(#cname))); \ |
| 147 i::Handle<i::Object> value = StdlibMathMember(isolate, stdlib, name); \ |
| 148 return !value.is_null() && value->IsNumber() && \ |
| 149 value->Number() == const_value; \ |
| 150 } |
| 151 STDLIB_MATH_CONST(E, 2.718281828459045) |
| 152 STDLIB_MATH_CONST(LN10, 2.302585092994046) |
| 153 STDLIB_MATH_CONST(LN2, 0.6931471805599453) |
| 154 STDLIB_MATH_CONST(LOG2E, 1.4426950408889634) |
| 155 STDLIB_MATH_CONST(LOG10E, 0.4342944819032518) |
| 156 STDLIB_MATH_CONST(PI, 3.141592653589793) |
| 157 STDLIB_MATH_CONST(SQRT1_2, 0.7071067811865476) |
| 158 STDLIB_MATH_CONST(SQRT2, 1.4142135623730951) |
| 159 #undef STDLIB_MATH_CONST |
115 default: { UNREACHABLE(); } | 160 default: { UNREACHABLE(); } |
116 } | 161 } |
117 return false; | 162 return false; |
118 } | 163 } |
119 | 164 |
120 } // namespace | 165 } // namespace |
121 | 166 |
122 MaybeHandle<FixedArray> AsmJs::ConvertAsmToWasm(ParseInfo* info) { | 167 MaybeHandle<FixedArray> AsmJs::ConvertAsmToWasm(ParseInfo* info) { |
123 ErrorThrower thrower(info->isolate(), "Asm.js -> WebAssembly conversion"); | 168 ErrorThrower thrower(info->isolate(), "Asm.js -> WebAssembly conversion"); |
124 wasm::AsmTyper typer(info->isolate(), info->zone(), *(info->script()), | 169 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()) { | 260 if (retval.is_null()) { |
216 thrower.Error( | 261 thrower.Error( |
217 "WASM.instantiateModuleFromAsm(): foreign init function failed"); | 262 "WASM.instantiateModuleFromAsm(): foreign init function failed"); |
218 return MaybeHandle<Object>(); | 263 return MaybeHandle<Object>(); |
219 } | 264 } |
220 return maybe_module_object; | 265 return maybe_module_object; |
221 } | 266 } |
222 | 267 |
223 } // namespace internal | 268 } // namespace internal |
224 } // namespace v8 | 269 } // namespace v8 |
OLD | NEW |