forked from facebook/hermes
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Summary: The previous attempt to allow internal bytecode had a bug that caused an assertion failure in some builds. Bring the code back, but add an ifdef for `HERMESVM_USE_JS_LIBRARY_IMPLEMENTATION` to avoid enabling it always. For now, it is off by default in CMake. Reviewed By: ridiculousfish Differential Revision: D17621584 fbshipit-source-id: 7967bcac08acc40a4975cf9a8f8f98c892e01340
- Loading branch information
1 parent
26ded02
commit 9636c07
Showing
20 changed files
with
296 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
*/ | ||
#ifndef HERMES_INTERNALBYTECODE_INTERNALBYTECODE_H | ||
#define HERMES_INTERNALBYTECODE_INTERNALBYTECODE_H | ||
|
||
/// \file | ||
/// InternalBytecode is a way to embed JS source into the VM at build time. | ||
/// The purpose of this is to implement parts of Hermes in JS. | ||
/// The build will concatenate multiple JS files together to form a single file, | ||
/// which will be compiled and turned into a static C array of bytes. | ||
/// Concatenating the files has multiple benefits: | ||
/// * Explicit control of the initialization order | ||
/// * Single string table and bytecode file | ||
/// * Explicit control over scoping and sharing | ||
/// * Cross-file inlining | ||
/// * Cross-file inlining | ||
/// * Better optimization because sharing occurs in local variables, | ||
/// not in object properties. | ||
|
||
#include "llvm/ADT/ArrayRef.h" | ||
|
||
#include <cstdint> | ||
#include <vector> | ||
|
||
namespace hermes { | ||
namespace vm { | ||
|
||
/// Get a pre-compiled bytecode module to be included with the VM upon | ||
/// construction. This module must be run before any user code can be run. | ||
/// \return A list of bytes that can be turned into a Hermes bytecode module. | ||
llvm::ArrayRef<uint8_t> getInternalBytecode(); | ||
|
||
} // namespace vm | ||
} // namespace hermes | ||
|
||
#endif // HERMES_INTERNALBYTECODE_INTERNALBYTECODE_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
# 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. | ||
|
||
add_definitions(-DHERMES_CMAKE_BUILD) | ||
|
||
if(CMAKE_CROSSCOMPILING) | ||
set(IMPORT_HERMESC "IMPORTFILE-NOTFOUND" CACHE FILEPATH "hermesc export file") | ||
file(TO_CMAKE_PATH "${IMPORT_HERMESC}" IMPORT_HERMESC_CMAKE) | ||
include(${IMPORT_HERMESC_CMAKE}) | ||
set(hermesc_EXE native-hermesc) | ||
else() | ||
set(hermesc_EXE hermesc) | ||
endif() | ||
|
||
add_llvm_library(hermesInternalBytecode | ||
InternalBytecode.cpp | ||
) | ||
|
||
# Only enable optimized libraries if the Hermes debugger isn't on. | ||
# This allows the user to debug the JS libraries. | ||
if(HERMES_ENABLE_DEBUGGER) | ||
set(JS_COMPILER_FLAGS "") | ||
else() | ||
set(JS_COMPILER_FLAGS "-O") | ||
endif() | ||
|
||
# Concatenate all JS files into one source file for compilation. | ||
# This way there is only one RuntimeModule made for them. | ||
# The concatenation order is set specifically so that order is controlled here. | ||
if(NOT WIN32) | ||
set(concatenate "cat") | ||
else() | ||
set(concatenate "type") | ||
endif() | ||
|
||
add_custom_command( | ||
OUTPUT JSLib.js | ||
COMMAND ${concatenate} Number.js > ${CMAKE_CURRENT_BINARY_DIR}/JSLib.js | ||
DEPENDS Number.js | ||
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} | ||
VERBATIM | ||
) | ||
|
||
add_custom_command( | ||
OUTPUT JSLib.hbc | ||
COMMAND ${hermesc_EXE} ${JS_COMPILER_FLAGS} -emit-binary -out=${CMAKE_CURRENT_BINARY_DIR}/JSLib.hbc ${CMAKE_CURRENT_BINARY_DIR}/JSLib.js | ||
DEPENDS ${hermesc_EXE} JSLib.js | ||
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} | ||
VERBATIM | ||
) | ||
|
||
# X.inc is a "C array" source file containing Hermes bytecode. | ||
# It's built by compiling X with hermesc, and then piping that through xxd -i. | ||
|
||
add_custom_command( | ||
OUTPUT InternalBytecode.inc | ||
COMMAND python3 xxd.py ${CMAKE_CURRENT_BINARY_DIR}/JSLib.hbc > ${CMAKE_CURRENT_BINARY_DIR}/InternalBytecode.inc | ||
DEPENDS xxd.py JSLib.hbc | ||
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} | ||
VERBATIM | ||
) | ||
|
||
set_source_files_properties(InternalBytecode.cpp | ||
PROPERTIES | ||
OBJECT_DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/InternalBytecode.inc | ||
) | ||
|
||
# Allow InternalBytecode to find its .inc file | ||
target_include_directories(hermesInternalBytecode | ||
PRIVATE ${CMAKE_CURRENT_BINARY_DIR} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
* 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. | ||
*/ | ||
#include "hermes/InternalBytecode/InternalBytecode.h" | ||
|
||
namespace hermes { | ||
namespace vm { | ||
|
||
llvm::ArrayRef<uint8_t> getInternalBytecode() { | ||
#ifdef HERMESVM_USE_JS_LIBRARY_IMPLEMENTATION | ||
static const uint8_t InternalBytecode[] = { | ||
#ifdef HERMES_CMAKE_BUILD | ||
#include "InternalBytecode.inc" | ||
#else | ||
#include "hermes/InternalBytecode/InternalBytecode.inc" | ||
#endif | ||
}; | ||
|
||
return llvm::makeArrayRef(InternalBytecode, sizeof(InternalBytecode)); | ||
#else | ||
return llvm::ArrayRef<uint8_t>{}; | ||
#endif | ||
} | ||
} // namespace vm | ||
} // namespace hermes |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// 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. | ||
|
||
(function() { | ||
'use strict'; | ||
|
||
var desc = { | ||
enumerable: false, | ||
writable: false, | ||
configurable: false, | ||
value: 9007199254740991, | ||
}; | ||
|
||
// ES6.0 20.1.2.6 | ||
Object.defineProperty(Number, 'MAX_SAFE_INTEGER', desc); | ||
|
||
desc.value = -desc.value; | ||
|
||
// ES6.0 20.1.2.8 | ||
Object.defineProperty(Number, 'MIN_SAFE_INTEGER', desc); | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
#!/usr/bin/env python3 | ||
# 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. | ||
|
||
""" | ||
This module exists so that a Windows build can have access to functionality | ||
like xxd -i | ||
""" | ||
|
||
import argparse | ||
from os import path | ||
|
||
|
||
# This is about 80 characters long (4 characters per byte + comma + space) | ||
BYTES_PER_LINE = 12 | ||
|
||
|
||
def main(): | ||
parser = argparse.ArgumentParser() | ||
parser.add_argument("file") | ||
args = parser.parse_args() | ||
|
||
# Ensure the file exists before writing out anything | ||
if not path.exists(args.file): | ||
raise Exception(f'File "{args.file}" doesn\'t exist') | ||
|
||
with open(args.file, "rb") as f: | ||
# Could read in chunks instead for extra performance, but this script | ||
# isn't meant to be used on gigantic files. | ||
file_as_bytes = f.read() | ||
|
||
# Make groups of bytes that fit in a single line | ||
lines = [ | ||
file_as_bytes[i : i + BYTES_PER_LINE] | ||
for i in range(0, len(file_as_bytes), BYTES_PER_LINE) | ||
] | ||
print(",\n".join(", ".join("0x{:02x}".format(b) for b in l) for l in lines)) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.