Skip to content

Commit

Permalink
Reapply "Add CLI/RuntimeConfig for conditionalizing Promise."
Browse files Browse the repository at this point in the history
Summary: Reapply D20106660

Reviewed By: tmikov

Differential Revision: D24371919

fbshipit-source-id: 230855d2691f9a819b546bca9a7fcc0c821246fd
  • Loading branch information
Huxpro authored and facebook-github-bot committed Oct 19, 2020
1 parent 04d7a3e commit 15ecd13
Show file tree
Hide file tree
Showing 15 changed files with 81 additions and 6 deletions.
1 change: 1 addition & 0 deletions API/hermes/SynthTrace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ SynthTrace::SynthTrace(
json_->closeDict();
}
json_->emitKeyValue("maxNumRegisters", conf.getMaxNumRegisters());
json_->emitKeyValue("ES6Promise", conf.getES6Promise());
json_->emitKeyValue("ES6Proxy", conf.getES6Proxy());
json_->emitKeyValue("ES6Symbol", conf.getES6Symbol());
json_->emitKeyValue("enableSampledStats", conf.getEnableSampledStats());
Expand Down
6 changes: 6 additions & 0 deletions include/hermes/ConsoleHost/RuntimeFlags.h
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,12 @@ static opt<std::string> ProfilerSymbolsFile(
cat(RuntimeCategory));
#endif

static opt<bool> ES6Promise(
"Xes6-promise",
desc("Enable support for ES6 Promise"),
init(RuntimeConfig::getDefaultES6Promise()),
cat(RuntimeCategory));

static opt<bool> ES6Proxy(
"Xes6-proxy",
desc("Enable support for ES6 Proxy"),
Expand Down
7 changes: 7 additions & 0 deletions include/hermes/VM/Runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -747,6 +747,10 @@ class Runtime : public HandleRootOwner,
return runtimeModuleList_;
}

bool hasES6Promise() const {
return hasES6Promise_;
}

bool hasES6Proxy() const {
return hasES6Proxy_;
}
Expand Down Expand Up @@ -993,6 +997,9 @@ class Runtime : public HandleRootOwner,
/// All state related to JIT compilation.
JITContext jitContext_;

/// Set to true if we should enable ES6 Promise.
const bool hasES6Promise_;

/// Set to true if we should enable ES6 Proxy.
const bool hasES6Proxy_;

Expand Down
1 change: 1 addition & 0 deletions include/hermes/VM/SerializeHeader.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ struct SerializeHeader {
/// Runtime has the same runtime config as the Deserialize system. Write those
/// flags that affects S/D in the header so we can check them too.
bool enableEval;
bool hasES6Promise;
bool hasES6Proxy;
bool hasES6Symbol;
uint8_t bytecodeWarmupPercent;
Expand Down
2 changes: 1 addition & 1 deletion lib/VM/JSLib/HermesInternal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -566,7 +566,7 @@ hermesInternalIsProxy(void *, Runtime *runtime, NativeArgs args) {

CallResult<HermesValue>
hermesInternalHasPromise(void *, Runtime *runtime, NativeArgs args) {
return HermesValue::encodeBoolValue(false);
return HermesValue::encodeBoolValue(runtime->hasES6Promise());
}

#ifdef HERMESVM_EXCEPTION_ON_OOM
Expand Down
6 changes: 6 additions & 0 deletions lib/VM/Runtime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ Runtime::Runtime(
runtimeConfig.getCrashMgr(),
std::move(provider)),
jitContext_(runtimeConfig.getEnableJIT(), (1 << 20) * 16, (1 << 20) * 32),
hasES6Promise_(runtimeConfig.getES6Promise()),
hasES6Proxy_(runtimeConfig.getES6Proxy()),
hasES6Symbol_(runtimeConfig.getES6Symbol()),
shouldRandomizeMemoryLayout_(runtimeConfig.getRandomizeMemoryLayout()),
Expand Down Expand Up @@ -2156,6 +2157,7 @@ void Runtime::deserializeImpl(Deserializer &d, bool currentlyInYoung) {

void Runtime::populateHeaderRuntimeConfig(SerializeHeader &header) {
header.enableEval = enableEval;
header.hasES6Promise = hasES6Promise_;
header.hasES6Proxy = hasES6Proxy_;
header.hasES6Symbol = hasES6Symbol_;
header.bytecodeWarmupPercent = bytecodeWarmupPercent_;
Expand All @@ -2167,6 +2169,10 @@ void Runtime::checkHeaderRuntimeConfig(SerializeHeader &header) const {
hermes_fatal(
"serialize/deserialize Runtime Configs don't match (enableEval)");
}
if (header.hasES6Promise != hasES6Promise_) {
hermes_fatal(
"serialize/deserialize Runtime Configs don't match (es6Promise)");
}
if (header.hasES6Proxy != hasES6Proxy_) {
hermes_fatal(
"serialize/deserialize Runtime Configs don't match (es6Proxy)");
Expand Down
3 changes: 3 additions & 0 deletions public/hermes/Public/RuntimeConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ class Deserializer;
/* Whether to optimize the code inside eval and Function ctor */ \
F(constexpr, bool, OptimizedEval, false) \
\
/* Support for ES6 Promise. */ \
F(constexpr, bool, ES6Promise, false) \
\
/* Support for ES6 Proxy. */ \
F(constexpr, bool, ES6Proxy, true) \
\
Expand Down
40 changes: 40 additions & 0 deletions test/hermes/promise.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

// RUN: %hermes -Xes6-promise %s | %FileCheck --match-full-lines %s
// RUN: %hermesc -O -emit-binary -out %t.hbc %s && %hermes -Xes6-promise %t.hbc | %FileCheck --match-full-lines %s

print('promise');
// CHECK-LABEL: promise

print(HermesInternal.hasPromise())
// CHECK-NEXT: true

var promise = new Promise(function(res, rej) {
res('success!');
});

promise.then(function(message) {
print('Resolved:', message);
});
// CHECK-NEXT: Resolved: success!

HermesInternal.enablePromiseRejectionTracker({
allRejections: true,
onUnhandled: function(id, error) {
print("Unhandled:", error);
},
});

var promise = new Promise(function(res, rej) {
rej("failure!");
});

promise.then(function() {
print('resolved');
});
// CHECK-NEXT: Unhandled: failure!
2 changes: 2 additions & 0 deletions tools/hermes/hermes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ static int executeHBCBytecodeFromCL(
.withVerifyEvalIR(cl::VerifyIR)
.withOptimizedEval(cl::OptimizedEval)
.withVMExperimentFlags(cl::VMExperimentFlags)
.withES6Promise(cl::ES6Promise)
.withES6Proxy(cl::ES6Proxy)
.withES6Symbol(cl::ES6Symbol)
.withEnableSampleProfiling(cl::SampleProfiling)
Expand Down Expand Up @@ -181,6 +182,7 @@ static vm::RuntimeConfig getReplRuntimeConfig() {
.build())
.withShouldRecordStats(cl::GCPrintStats)
.build())
.withES6Promise(cl::ES6Promise)
.withES6Proxy(cl::ES6Proxy)
.withES6Symbol(cl::ES6Symbol)
.withEnableHermesInternal(true)
Expand Down
1 change: 1 addition & 0 deletions tools/hvm/hvm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ int main(int argc, char **argv) {
.withShouldReleaseUnused(vm::kReleaseUnusedNone)
.withName("hvm")
.build())
.withES6Promise(cl::ES6Promise)
.withES6Proxy(cl::ES6Proxy)
.withES6Symbol(cl::ES6Symbol)
.withTrackIO(cl::TrackBytecodeIO)
Expand Down
8 changes: 5 additions & 3 deletions unittests/API/APITest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,10 @@ class HermesRuntimeTestBase : public ::testing::Test {
class HermesRuntimeTest : public HermesRuntimeTestBase {
public:
HermesRuntimeTest()
: HermesRuntimeTestBase(
::hermes::vm::RuntimeConfig::Builder().withES6Proxy(true).build()) {
}
: HermesRuntimeTestBase(::hermes::vm::RuntimeConfig::Builder()
.withES6Proxy(true)
.withES6Promise(true)
.build()) {}
};

using HermesRuntimeDeathTest = HermesRuntimeTest;
Expand Down Expand Up @@ -466,6 +467,7 @@ class HermesRuntimeTestWithAllowFunctionToString
: HermesRuntimeTestBase(
::hermes::vm::RuntimeConfig::Builder()
.withES6Proxy(true)
.withES6Promise(true)
.withAllowFunctionToStringWithRuntimeSource(true)
.build()) {}
};
Expand Down
4 changes: 3 additions & 1 deletion unittests/API/SynthTraceParserTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ TEST_F(SynthTraceParserTest, ParseHeader) {
"allocInYoung": false,
},
"maxNumRegisters": 100,
"ES6Proxy": true,
"ES6Promise": true,
"ES6Proxy": false,
"ES6Symbol": false,
"enableSampledStats": true,
"vmExperimentFlags": 123
Expand Down Expand Up @@ -71,6 +72,7 @@ TEST_F(SynthTraceParserTest, ParseHeader) {
EXPECT_FALSE(gcconf.getAllocInYoung());

EXPECT_EQ(rtconf.getMaxNumRegisters(), 100);
EXPECT_FALSE(rtconf.getES6Promise());
EXPECT_TRUE(rtconf.getES6Proxy());
EXPECT_FALSE(rtconf.getES6Symbol());
EXPECT_TRUE(rtconf.getEnableSampledStats());
Expand Down
3 changes: 3 additions & 0 deletions unittests/API/SynthTraceTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1234,6 +1234,9 @@ TEST_F(SynthTraceSerializationTest, TraceHeader) {
EXPECT_EQ(
conf.getMaxNumRegisters(),
llvh::cast<JSONNumber>(rtConfig->at("maxNumRegisters"))->getValue());
EXPECT_EQ(
conf.getES6Promise(),
llvh::cast<JSONBoolean>(rtConfig->at("ES6Promise"))->getValue());
EXPECT_EQ(
conf.getES6Proxy(),
llvh::cast<JSONBoolean>(rtConfig->at("ES6Proxy"))->getValue());
Expand Down
1 change: 1 addition & 0 deletions unittests/VMRuntime/StackTracesTreeTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ struct StackTracesTreeTest : public RuntimeTestFixtureBase {
explicit StackTracesTreeTest()
: RuntimeTestFixtureBase(
RuntimeConfig::Builder(kTestRTConfigBuilder)
.withES6Promise(true)
.withES6Proxy(true)
.withGCConfig(GCConfig::Builder(kTestGCConfigBuilder).build())
.build()) {
Expand Down
2 changes: 1 addition & 1 deletion utils/testsuite/testsuite.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ def showStatus(filename):
print("Testing " + filename)


es6_args = ["-Xes6-proxy", "-Xes6-symbol"]
es6_args = ["-Xes6-promise", "-Xes6-proxy", "-Xes6-symbol"]
extra_run_args = ["-Xhermes-internal-test-methods"]

extra_compile_flags = ["-fno-static-builtins"]
Expand Down

0 comments on commit 15ecd13

Please sign in to comment.