Skip to content

Commit

Permalink
Delete allowFunctionToStringWithRuntimeSource
Browse files Browse the repository at this point in the history
Summary:
This diff deprecate an old mechanism to allow `Function$toString`
to use source code if the JS was compiled at runtime (eval, lazy),
in which case the source code is available at runtime. However,
this mechanism cannot be scaled to JS functions pre-compiled
to HBC.

In later diff, we'll introduce an new mechanism which reserves
function source through HBC so it's general enough to be working
for both the eager and lazy compilation.

Reviewed By: avp

Differential Revision: D29093332

fbshipit-source-id: 932b4c588f521bd13adba43f34347d255529bc70
  • Loading branch information
Huxpro authored and facebook-github-bot committed Jul 7, 2021
1 parent 1590882 commit fb8c5a5
Show file tree
Hide file tree
Showing 17 changed files with 97 additions and 314 deletions.
13 changes: 0 additions & 13 deletions include/hermes/AST/Context.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,6 @@ class Context {
/// bytes.
unsigned preemptiveFileCompilationThreshold_{0};

/// Allows Function.toString() to return original source code. As with lazy
/// compilation this requires source buffers, and hence this Context instance
/// to be retained after compilation.
bool allowFunctionToStringWithRuntimeSource_{false};

/// If true, do not error on return statements that are not within functions.
bool allowReturnOutsideFunction_{false};

Expand Down Expand Up @@ -349,14 +344,6 @@ class Context {
preemptiveFileCompilationThreshold_ = byteCount;
};

bool allowFunctionToStringWithRuntimeSource() const {
return allowFunctionToStringWithRuntimeSource_;
}

void setAllowFunctionToStringWithRuntimeSource(bool v) {
allowFunctionToStringWithRuntimeSource_ = v;
}

bool allowReturnOutsideFunction() const {
return allowReturnOutsideFunction_;
}
Expand Down
1 change: 0 additions & 1 deletion include/hermes/BCGen/HBC/BytecodeProviderFromSrc.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ struct CompileFlags {
/// Eagerly compile functions under this number of bytes, even when lazy.
unsigned preemptiveFunctionCompilationThreshold{160};

bool allowFunctionToStringWithRuntimeSource{false};
bool strict{false};
/// The value is optional; when it is set, the optimization setting is based
/// on the value; when it is unset, it means the parser needs to automatically
Expand Down
1 change: 0 additions & 1 deletion include/hermes/CompilerDriver/CompilerDriver.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ extern llvh::cl::opt<bool> BasicBlockProfiling;
extern llvh::cl::opt<bool> EnableEval;
extern llvh::cl::opt<bool> VerifyIR;
extern llvh::cl::opt<bool> EmitAsyncBreakCheck;
extern llvh::cl::opt<bool> AllowFunctionToString;
extern llvh::cl::list<std::string> InputFilenames;
extern llvh::cl::opt<bool> OptimizedEval;
extern llvh::cl::opt<bool> EmitAsyncBreakCheck;
Expand Down
11 changes: 0 additions & 11 deletions include/hermes/VM/CodeBlock.h
Original file line number Diff line number Diff line change
Expand Up @@ -312,17 +312,6 @@ class CodeBlock final
/// \param runtimeModule The RuntimeModule the CodeBlock belongs to.
static CodeBlock *deserialize(Deserializer &d, RuntimeModule *runtimeModule);
#endif

#ifndef HERMESVM_LEAN
/// Returns true if source code is available for the function backing this
/// CodeBlock. This will only be the case if the function is lazily compiled,
/// or we've enabled Function.toString() to return source and compilation was
/// done at run-time.
llvh::StringRef getFunctionSource() const;

/// Returns true if \c getFunctionSource() above will return function source.
bool hasFunctionSource() const;
#endif
};

} // namespace vm
Expand Down
15 changes: 2 additions & 13 deletions include/hermes/VM/Runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ class Runtime : public HandleRootOwner,
CallResult<HermesValue> run(
llvh::StringRef code,
llvh::StringRef sourceURL,
hbc::CompileFlags compileFlags);
const hbc::CompileFlags &compileFlags);

/// Runs the given UTF-8 \p code in a new RuntimeModule as top-level code.
/// \param sourceURL the location of the source that's being run.
Expand All @@ -247,7 +247,7 @@ class Runtime : public HandleRootOwner,
CallResult<HermesValue> run(
std::unique_ptr<Buffer> code,
llvh::StringRef sourceURL,
hbc::CompileFlags compileFlags);
const hbc::CompileFlags &compileFlags);

/// Runs the given \p bytecode with the given \p runtimeModuleFlags. The \p
/// sourceURL, if not empty, is reported as the file name in backtraces. If \p
Expand Down Expand Up @@ -1332,9 +1332,6 @@ class Runtime : public HandleRootOwner,
/// Config-provided callback for GC events.
std::function<void(GCEventKind, const char *)> gcEventCallback_;

/// Set from RuntimeConfig.
bool allowFunctionToStringWithRuntimeSource_;

/// @}

/// \return whether any async break is requested or not.
Expand Down Expand Up @@ -1440,14 +1437,6 @@ class Runtime : public HandleRootOwner,
HermesValue::encodeNativePointer(getCurrentIP());
}

void setAllowFunctionToStringWithRuntimeSource(bool v) {
allowFunctionToStringWithRuntimeSource_ = v;
}

bool getAllowFunctionToStringWithRuntimeSource() const {
return allowFunctionToStringWithRuntimeSource_;
}

private:
/// Given the current last known IP used in the interpreter loop, returns the
/// last known CodeBlock and IP combination. IP must not be null as this
Expand Down
5 changes: 1 addition & 4 deletions lib/BCGen/HBC/BytecodeGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -292,10 +292,7 @@ std::unique_ptr<BytecodeModule> BytecodeModuleGenerator::generate() {
functionNameId);

#ifndef HERMESVM_LEAN
if (F->getParent()
->shareContext()
->allowFunctionToStringWithRuntimeSource() ||
F->isLazy()) {
if (F->isLazy()) {
auto context = F->getParent()->shareContext();
assert(
(!contextIfNeeded || contextIfNeeded.get() == context.get()) &&
Expand Down
2 changes: 0 additions & 2 deletions lib/BCGen/HBC/BytecodeProviderFromSrc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,6 @@ BCProviderFromSrc::createBCProviderFromSrc(
context->setLazyCompilation(true);
}

context->setAllowFunctionToStringWithRuntimeSource(
compileFlags.allowFunctionToStringWithRuntimeSource);
context->setGeneratorEnabled(compileFlags.enableGenerator);
context->setDebugInfoSetting(
compileFlags.debug ? DebugInfoSetting::ALL : DebugInfoSetting::THROWING);
Expand Down
9 changes: 0 additions & 9 deletions lib/CompilerDriver/CompilerDriver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -307,11 +307,6 @@ opt<bool> EmitAsyncBreakCheck(
init(false),
cat(CompilerCategory));

opt<bool> AllowFunctionToString(
"allow-function-to-string",
init(false),
desc("Enables Function.toString() to return source-code when available"));

opt<bool> OptimizedEval(
"optimized-eval",
desc("Turn on compiler optimizations in eval."),
Expand Down Expand Up @@ -1819,10 +1814,6 @@ CompileResult processSourceFiles(
}
}

// Allows Function.toString() to return original source code. As with lazy
// compilation this requires source buffers to be retained after compilation.
context->setAllowFunctionToStringWithRuntimeSource(cl::AllowFunctionToString);

// Create the source map if requested.
llvh::Optional<SourceMapGenerator> sourceMapGen{};
if (cl::OutputSourceMap) {
Expand Down
16 changes: 0 additions & 16 deletions lib/VM/CodeBlock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -282,22 +282,6 @@ SourceErrorManager::SourceCoords CodeBlock::getLazyFunctionLoc(
return coords;
}

#ifndef HERMESVM_LEAN
bool CodeBlock::hasFunctionSource() const {
return runtimeModule_->getBytecode()
->getFunctionSourceRange(functionID_)
.isValid();
}

llvh::StringRef CodeBlock::getFunctionSource() const {
auto sourceRange =
runtimeModule_->getBytecode()->getFunctionSourceRange(functionID_);
assert(sourceRange.isValid() && "Function does not have source available");
const auto sourceStart = sourceRange.Start.getPointer();
return {sourceStart, (size_t)(sourceRange.End.getPointer() - sourceStart)};
}
#endif

#ifndef HERMESVM_LEAN
namespace {
std::unique_ptr<hbc::BytecodeModule> compileLazyFunction(
Expand Down
32 changes: 0 additions & 32 deletions lib/VM/JSLib/Function.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
#include "hermes/VM/StringBuilder.h"
#include "hermes/VM/StringView.h"

#include "llvh/Support/ConvertUTF.h"

namespace hermes {
namespace vm {

Expand Down Expand Up @@ -101,36 +99,6 @@ functionPrototypeToString(void *, Runtime *runtime, NativeArgs args) {
"Can't call Function.prototype.toString() on non-callable");
}

#ifndef HERMESVM_LEAN
JSFunction *jsFunc;
if (runtime->getAllowFunctionToStringWithRuntimeSource() &&
(jsFunc = dyn_vmcast<JSFunction>(*func)) &&
jsFunc->getCodeBlock()->hasFunctionSource()) {
auto code = jsFunc->getCodeBlock()->getFunctionSource();
if (isAllASCII(std::begin(code), std::end(code))) {
return vm::StringPrimitive::createEfficient(
runtime, llvh::makeArrayRef(std::begin(code), std::end(code)));
}
std::u16string out;
out.resize(code.size());
const llvh::UTF8 *sourceStart = (const llvh::UTF8 *)code.data();
const llvh::UTF8 *sourceEnd = sourceStart + code.size();
llvh::UTF16 *targetStart = (llvh::UTF16 *)&out[0];
llvh::UTF16 *targetEnd = targetStart + out.size();
auto cRes = ConvertUTF8toUTF16(
&sourceStart,
sourceEnd,
&targetStart,
targetEnd,
llvh::lenientConversion);
(void)cRes;
assert(
cRes != llvh::ConversionResult::targetExhausted &&
"not enough space allocated for UTF16 conversion");
return vm::StringPrimitive::createEfficient(runtime, std::move(out));
}
#endif

SmallU16String<64> strBuf{};
if (vmisa<JSAsyncFunction>(*func)) {
strBuf.append("async function ");
Expand Down
5 changes: 1 addition & 4 deletions lib/VM/JSLib/eval.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@ CallResult<HermesValue> evalInEnvironment(
compileFlags.emitAsyncBreakCheck = runtime->asyncBreakCheckInEval;
compileFlags.lazy =
utf8code.size() >= compileFlags.preemptiveFileCompilationThreshold;
compileFlags.allowFunctionToStringWithRuntimeSource =
runtime->getAllowFunctionToStringWithRuntimeSource();
#ifdef HERMES_ENABLE_DEBUGGER
// Required to allow stepping and examining local variables in eval'd code
compileFlags.debug = true;
Expand All @@ -60,8 +58,7 @@ CallResult<HermesValue> evalInEnvironment(
std::unique_ptr<hbc::BCProviderFromSrc> bytecode;
{
std::unique_ptr<hermes::Buffer> buffer;
if (compileFlags.lazy ||
compileFlags.allowFunctionToStringWithRuntimeSource) {
if (compileFlags.lazy) {
buffer.reset(new hermes::OwnedMemoryBuffer(
llvh::MemoryBuffer::getMemBufferCopy(utf8code)));
} else {
Expand Down
15 changes: 4 additions & 11 deletions lib/VM/Runtime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,7 @@ Runtime::Runtime(
crashCallbackKey_(
crashMgr_->registerCallback([this](int fd) { crashCallback(fd); })),
codeCoverageProfiler_(std::make_unique<CodeCoverageProfiler>(this)),
gcEventCallback_(runtimeConfig.getGCConfig().getCallback()),
allowFunctionToStringWithRuntimeSource_(
runtimeConfig.getAllowFunctionToStringWithRuntimeSource()) {
gcEventCallback_(runtimeConfig.getGCConfig().getCallback()) {
assert(
(void *)this == (void *)(HandleRootOwner *)this &&
"cast to HandleRootOwner should be no-op");
Expand Down Expand Up @@ -838,15 +836,12 @@ static CallResult<HermesValue> interpretFunctionWithRandomStack(
CallResult<HermesValue> Runtime::run(
llvh::StringRef code,
llvh::StringRef sourceURL,
hbc::CompileFlags compileFlags) {
const hbc::CompileFlags &compileFlags) {
#ifdef HERMESVM_LEAN
return raiseEvalUnsupported(code);
#else
compileFlags.allowFunctionToStringWithRuntimeSource |=
getAllowFunctionToStringWithRuntimeSource();
std::unique_ptr<hermes::Buffer> buffer;
if (compileFlags.lazy ||
compileFlags.allowFunctionToStringWithRuntimeSource) {
if (compileFlags.lazy) {
buffer.reset(new hermes::OwnedMemoryBuffer(
llvh::MemoryBuffer::getMemBufferCopy(code)));
} else {
Expand All @@ -860,14 +855,12 @@ CallResult<HermesValue> Runtime::run(
CallResult<HermesValue> Runtime::run(
std::unique_ptr<hermes::Buffer> code,
llvh::StringRef sourceURL,
hbc::CompileFlags compileFlags) {
const hbc::CompileFlags &compileFlags) {
#ifdef HERMESVM_LEAN
auto buffer = code.get();
return raiseEvalUnsupported(llvh::StringRef(
reinterpret_cast<const char *>(buffer->data()), buffer->size()));
#else
compileFlags.allowFunctionToStringWithRuntimeSource |=
getAllowFunctionToStringWithRuntimeSource();
std::unique_ptr<hbc::BCProviderFromSrc> bytecode;
{
PerfSection loading("Loading new JavaScript code");
Expand Down

0 comments on commit fb8c5a5

Please sign in to comment.