Skip to content

Commit

Permalink
Refactor HBC test helper
Browse files Browse the repository at this point in the history
Summary:
Provide a new API in the test helper, `bytecodeModuleForSource`. This
will return the module rather than the bytecode itself. With this new
API, refactor `bytecodeForSource` to internally call into it. We will be
using `bytecodeModuleForSource` in the next diff for testing purposes.

Reviewed By: tmikov

Differential Revision: D41670281

fbshipit-source-id: 2a6d7fb909a171a279aabcc69333ee09845d9ddd
  • Loading branch information
Michael Anthony Leon authored and Riccardo Cipolleschi committed Mar 7, 2023
1 parent ee25883 commit 31fdcf7
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 21 deletions.
10 changes: 5 additions & 5 deletions unittests/BCGen/HBC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -515,12 +515,12 @@ TEST(HBCBytecodeGen, BytecodeFieldsFail) {
}

TEST(HBCBytecodeGen, SerializeBytecodeOptions) {
TestCompileFlags flags;
flags.staticBuiltins = false;
auto bytecodeVecDefault = bytecodeForSource("print('hello world');", flags);
flags.staticBuiltins = true;
BytecodeGenerationOptions opts = BytecodeGenerationOptions::defaults();
opts.staticBuiltinsEnabled = false;
auto bytecodeVecDefault = bytecodeForSource("print('hello world');", opts);
opts.staticBuiltinsEnabled = true;
auto bytecodeVecStaticBuiltins =
bytecodeForSource("print('hello world');", flags);
bytecodeForSource("print('hello world');", opts);

auto bytecodeDefault = hbc::BCProviderFromBuffer::createBCProviderFromBuffer(
std::make_unique<VectorBuffer>(bytecodeVecDefault))
Expand Down
29 changes: 17 additions & 12 deletions unittests/BCGen/TestHelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,28 @@

#include "TestHelpers.h"
#include "hermes/AST/SemValidate.h"
#include "hermes/BCGen/HBC/HBC.h"
#include "hermes/BCGen/HBC/TraverseLiteralStrings.h"
#include "hermes/BCGen/HBC/UniquingStringLiteralTable.h"
#include "hermes/IRGen/IRGen.h"
#include "hermes/Parser/JSParser.h"
#include "hermes/Utils/Options.h"
#include "llvh/Support/SHA1.h"

#include "gtest/gtest.h"

using namespace hermes;
using namespace hermes::hbc;

/// Compile source code \p source into Hermes bytecode.
/// \return the bytecode as a vector of bytes.
std::vector<uint8_t> hermes::bytecodeForSource(
/// Compile source code \p source into Hermes bytecode module.
/// \return the bytecode module.
std::unique_ptr<BytecodeModule> hermes::bytecodeModuleForSource(
const char *source,
TestCompileFlags flags) {
BytecodeGenerationOptions opts) {
/* Parse source */
SourceErrorManager sm;
CodeGenerationSettings codeGenOpts;
codeGenOpts.unlimitedRegisters = false;
OptimizationSettings optSettings;
optSettings.staticBuiltins = flags.staticBuiltins;
optSettings.staticBuiltins = opts.staticBuiltinsEnabled;
auto context = std::make_shared<Context>(sm, codeGenOpts, optSettings);
parser::JSParser jsParser(*context, source);
auto parsed = jsParser.parse();
Expand All @@ -47,16 +45,23 @@ std::vector<uint8_t> hermes::bytecodeForSource(
hermes::generateIRFromESTree(ast, &M, declFileList, {});

/* Generate bytecode module */
auto bytecodeGenOpts = BytecodeGenerationOptions::defaults();
bytecodeGenOpts.staticBuiltinsEnabled = flags.staticBuiltins;
auto BM =
generateBytecodeModule(&M, M.getTopLevelFunction(), bytecodeGenOpts);
auto BM = generateBytecodeModule(&M, M.getTopLevelFunction(), opts);
assert(BM != nullptr && "Failed to generate bytecode module");

return BM;
}

/// Compile source code \p source into Hermes bytecode.
/// \return the bytecode as a vector of bytes.
std::vector<uint8_t> hermes::bytecodeForSource(
const char *source,
BytecodeGenerationOptions opts) {
auto BM = bytecodeModuleForSource(source, opts);

/* Serialize it */
llvh::SmallVector<char, 0> bytecodeVector;
llvh::raw_svector_ostream OS(bytecodeVector);
BytecodeSerializer BS{OS, bytecodeGenOpts};
BytecodeSerializer BS{OS, opts};
BS.serialize(
*BM,
llvh::SHA1::hash(llvh::ArrayRef<uint8_t>{
Expand Down
11 changes: 7 additions & 4 deletions unittests/BCGen/TestHelpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,22 @@

#include <stdint.h>
#include <vector>
#include "hermes/BCGen/HBC/HBC.h"
#include "llvh/ADT/SmallVector.h"

namespace hermes {

struct TestCompileFlags {
bool staticBuiltins{false};
};
/// Compile source code \p source into Hermes bytecode module, asserting that it
/// can be compiled successfully. \return the bytecode module.
std::unique_ptr<hbc::BytecodeModule> bytecodeModuleForSource(
const char *source,
BytecodeGenerationOptions opts = BytecodeGenerationOptions::defaults());

/// Compile source code \p source into Hermes bytecode, asserting that it can be
/// compiled successfully. \return the bytecode as a vector of bytes.
std::vector<uint8_t> bytecodeForSource(
const char *source,
TestCompileFlags flags = TestCompileFlags());
BytecodeGenerationOptions opts = BytecodeGenerationOptions::defaults());

} // namespace hermes

Expand Down

0 comments on commit 31fdcf7

Please sign in to comment.