Index: src/asmjs/asm-js.cc |
diff --git a/src/asmjs/asm-js.cc b/src/asmjs/asm-js.cc |
index cb44ec237a2f3c48267d1d834fb3b6a2ee4b867f..c38c0537358b1f5f38606c05af9efcc568fedb2a 100644 |
--- a/src/asmjs/asm-js.cc |
+++ b/src/asmjs/asm-js.cc |
@@ -53,6 +53,70 @@ i::MaybeHandle<i::FixedArray> CompileModule( |
return compiled_module; |
} |
+bool IsStdlibMemberValid(i::Isolate* isolate, Handle<JSReceiver> stdlib, |
+ i::Handle<i::Object> member_id) { |
+ int32_t member_kind; |
+ if (!member_id->ToInt32(&member_kind)) { |
+ UNREACHABLE(); |
+ } |
+ switch (member_kind) { |
+ case wasm::AsmTyper::StandardMember::kNone: |
+ case wasm::AsmTyper::StandardMember::kModule: |
+ case wasm::AsmTyper::StandardMember::kStdlib: { |
+ // Nothing to check for these. |
+ return true; |
+ } |
+ case wasm::AsmTyper::StandardMember::kNaN: { |
+ i::Handle<i::Name> name(isolate->factory()->InternalizeOneByteString( |
+ STATIC_CHAR_VECTOR("NaN"))); |
+ i::MaybeHandle<i::Object> maybe_value = |
+ i::Object::GetProperty(stdlib, name); |
+ if (maybe_value.is_null()) { |
+ return false; |
+ } |
+ i::Handle<i::Object> value = maybe_value.ToHandleChecked(); |
+ if (!value->IsNaN()) { |
+ return false; |
+ } |
+ return true; |
+ } |
+ case wasm::AsmTyper::StandardMember::kHeap: |
+ case wasm::AsmTyper::StandardMember::kFFI: |
+ case wasm::AsmTyper::StandardMember::kInfinity: |
+ case wasm::AsmTyper::StandardMember::kMathAcos: |
+ case wasm::AsmTyper::StandardMember::kMathAsin: |
+ case wasm::AsmTyper::StandardMember::kMathAtan: |
+ case wasm::AsmTyper::StandardMember::kMathCos: |
+ case wasm::AsmTyper::StandardMember::kMathSin: |
+ case wasm::AsmTyper::StandardMember::kMathTan: |
+ case wasm::AsmTyper::StandardMember::kMathExp: |
+ case wasm::AsmTyper::StandardMember::kMathLog: |
+ case wasm::AsmTyper::StandardMember::kMathCeil: |
+ case wasm::AsmTyper::StandardMember::kMathFloor: |
+ case wasm::AsmTyper::StandardMember::kMathSqrt: |
+ case wasm::AsmTyper::StandardMember::kMathAbs: |
+ case wasm::AsmTyper::StandardMember::kMathClz32: |
+ case wasm::AsmTyper::StandardMember::kMathMin: |
+ case wasm::AsmTyper::StandardMember::kMathMax: |
+ case wasm::AsmTyper::StandardMember::kMathAtan2: |
+ case wasm::AsmTyper::StandardMember::kMathPow: |
+ case wasm::AsmTyper::StandardMember::kMathImul: |
+ case wasm::AsmTyper::StandardMember::kMathFround: |
+ case wasm::AsmTyper::StandardMember::kMathE: |
+ case wasm::AsmTyper::StandardMember::kMathLN10: |
+ case wasm::AsmTyper::StandardMember::kMathLN2: |
+ case wasm::AsmTyper::StandardMember::kMathLOG2E: |
+ case wasm::AsmTyper::StandardMember::kMathLOG10E: |
+ case wasm::AsmTyper::StandardMember::kMathPI: |
+ case wasm::AsmTyper::StandardMember::kMathSQRT1_2: |
+ case wasm::AsmTyper::StandardMember::kMathSQRT2: |
+ // TODO(bradnelson) Actually check these. |
+ return true; |
+ default: { UNREACHABLE(); } |
+ } |
+ return false; |
+} |
+ |
} // namespace |
MaybeHandle<FixedArray> AsmJs::ConvertAsmToWasm(ParseInfo* info) { |
@@ -74,16 +138,37 @@ MaybeHandle<FixedArray> AsmJs::ConvertAsmToWasm(ParseInfo* info) { |
internal::wasm::kAsmJsOrigin); |
DCHECK(!compiled.is_null()); |
- Handle<FixedArray> result = info->isolate()->factory()->NewFixedArray(2); |
+ wasm::AsmTyper::StdlibSet uses = typer.StdlibUses(); |
+ Handle<FixedArray> uses_array = |
+ info->isolate()->factory()->NewFixedArray(static_cast<int>(uses.size())); |
+ int count = 0; |
+ for (auto i : uses) { |
+ uses_array->set(count++, Smi::FromInt(i)); |
+ } |
+ |
+ Handle<FixedArray> result = info->isolate()->factory()->NewFixedArray(3); |
result->set(0, *compiled.ToHandleChecked()); |
result->set(1, *foreign_globals); |
+ result->set(2, *uses_array); |
return result; |
} |
+bool AsmJs::IsStdlibValid(i::Isolate* isolate, Handle<FixedArray> wasm_data, |
+ Handle<JSReceiver> stdlib) { |
+ i::Handle<i::FixedArray> uses(i::FixedArray::cast(wasm_data->get(2))); |
+ for (int i = 0; i < uses->length(); ++i) { |
+ if (!IsStdlibMemberValid(isolate, stdlib, |
+ uses->GetValueChecked<i::Object>(isolate, i))) { |
+ return false; |
+ } |
+ } |
+ return true; |
+} |
+ |
MaybeHandle<Object> AsmJs::InstantiateAsmWasm(i::Isolate* isolate, |
Handle<FixedArray> wasm_data, |
Handle<JSArrayBuffer> memory, |
- Handle<JSObject> foreign) { |
+ Handle<JSReceiver> foreign) { |
i::Handle<i::FixedArray> compiled(i::FixedArray::cast(wasm_data->get(0))); |
i::Handle<i::FixedArray> foreign_globals( |
i::FixedArray::cast(wasm_data->get(1))); |