Skip to content

Commit

Permalink
Switch arrayPrototypeFind to use void *ctx as an integer instead of a…
Browse files Browse the repository at this point in the history
… pointer

Summary:
Use void *ctx as an interger in arrayPrototypeFind so its value can be retained
through S/D. Add NumKinds in IterationKind for sanity check.

Reviewed By: avp

Differential Revision: D16189506

fbshipit-source-id: b63ab3d2f3c48a06212b199368ed65d73b182ba9
  • Loading branch information
Lun-Liu authored and facebook-github-bot committed Jul 15, 2019
1 parent 72cc1fc commit 3cd3498
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 11 deletions.
1 change: 1 addition & 0 deletions include/hermes/VM/IterationKind.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ enum class IterationKind {
Key,
Value,
Entry,
NumKinds,
};

} // namespace vm
Expand Down
3 changes: 3 additions & 0 deletions include/hermes/VM/JSMapImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,9 @@ class JSMapIteratorImpl final : public JSObject {
value = arrHandle.getHermesValue();
break;
};
case IterationKind::NumKinds:
llvm_unreachable("Invalid iteration kind");
return HermesValue::encodeEmptyValue();
}
} else {
// If the next element in the iterator is invalid, we have
Expand Down
6 changes: 5 additions & 1 deletion lib/VM/JSArray.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -759,7 +759,7 @@ CallResult<HermesValue> JSArrayIterator::nextElement(
// 16. If itemKind is "value", let result be elementValue.
return createIterResultObject(runtime, valueHandle, false)
.getHermesValue();
case IterationKind::Entry:
case IterationKind::Entry: {
// 17. b. Let result be CreateArrayFromList(«index, elementValue»).
auto resultRes = JSArray::create(runtime, 2, 2);
if (LLVM_UNLIKELY(resultRes == ExecutionStatus::EXCEPTION)) {
Expand All @@ -770,6 +770,10 @@ CallResult<HermesValue> JSArrayIterator::nextElement(
JSArray::setElementAt(result, runtime, 1, valueHandle);
// 18. Return CreateIterResultObject(result, false).
return createIterResultObject(runtime, result, false).getHermesValue();
}
case IterationKind::NumKinds:
llvm_unreachable("Invalid iteration kind");
return HermesValue::encodeEmptyValue();
}

llvm_unreachable("Invalid iteration kind");
Expand Down
19 changes: 9 additions & 10 deletions lib/VM/JSLib/Array.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ Handle<JSObject> createArrayConstructor(Runtime *runtime) {
arrayPrototype,
Predefined::getSymbolID(Predefined::findIndex),
// Pass a non-null pointer here to indicate we're finding the index.
reinterpret_cast<void *>(arrayPrototypeFind),
(void *)true,
arrayPrototypeFind,
1);
defineMethod(
Expand All @@ -339,29 +339,25 @@ Handle<JSObject> createArrayConstructor(Runtime *runtime) {
arrayPrototypeIncludes,
1);

static IterationKind iterationKindKey = IterationKind::Key;
static IterationKind iterationKindValue = IterationKind::Value;
static IterationKind iterationKindEntry = IterationKind::Entry;

defineMethod(
runtime,
arrayPrototype,
Predefined::getSymbolID(Predefined::keys),
&iterationKindKey,
(void *)IterationKind::Key,
arrayPrototypeIterator,
0);
defineMethod(
runtime,
arrayPrototype,
Predefined::getSymbolID(Predefined::values),
&iterationKindValue,
(void *)IterationKind::Value,
arrayPrototypeIterator,
0);
defineMethod(
runtime,
arrayPrototype,
Predefined::getSymbolID(Predefined::entries),
&iterationKindEntry,
(void *)IterationKind::Entry,
arrayPrototypeIterator,
0);

Expand Down Expand Up @@ -3080,13 +3076,16 @@ arrayPrototypeIncludes(void *, Runtime *runtime, NativeArgs args) {

static CallResult<HermesValue>
arrayPrototypeIterator(void *ctx, Runtime *runtime, NativeArgs args) {
IterationKind kind = *reinterpret_cast<IterationKind *>(&ctx);
assert(
kind < IterationKind::NumKinds &&
"arrayPrototypeIterator with wrong kind");
auto objRes = toObject(runtime, args.getThisHandle());
if (LLVM_UNLIKELY(objRes == ExecutionStatus::EXCEPTION)) {
return ExecutionStatus::EXCEPTION;
}
auto obj = runtime->makeHandle<JSObject>(*objRes);
return JSArrayIterator::create(
runtime, obj, *reinterpret_cast<IterationKind *>(ctx));
return JSArrayIterator::create(runtime, obj, kind);
}

/// ES6.0 22.1.2.1 Array.from ( items [ , mapfn [ , thisArg ] ] )
Expand Down

0 comments on commit 3cd3498

Please sign in to comment.