Skip to content

Commit

Permalink
Remove dev-mode Hermes bytecode experiment
Browse files Browse the repository at this point in the history
Summary:
Changelog: [General][Removed] Remove experimental support for loading bytecode from Metro

Removes the experimental bundling strategy that offloads Hermes bytecode compilation to the packager server. The React Native parts of this experiment were never part of the public API, and the Metro parts never fully shipped in open source.

Followup from D43597007.

Reviewed By: robhogan

Differential Revision: D43604705

fbshipit-source-id: db3be553750ccbf286d876f75858299c5b750f19
  • Loading branch information
motiz88 authored and facebook-github-bot committed Mar 21, 2023
1 parent 6dcdb93 commit 6abc097
Show file tree
Hide file tree
Showing 15 changed files with 10 additions and 107 deletions.
5 changes: 1 addition & 4 deletions BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ load(
"//tools/build_defs/oss:rn_defs.bzl",
"ANDROID",
"APPLE",
"HERMES_BYTECODE_VERSION",
"IOS",
"RCT_IMAGE_DATA_DECODER_SOCKET",
"RCT_IMAGE_URL_LOADER_SOCKET",
Expand Down Expand Up @@ -429,9 +428,7 @@ rn_apple_xplat_cxx_library(
"linux",
["-D PIC_MODIFIER=@PLT"],
)],
preprocessor_flags = get_objc_arc_preprocessor_flags() + get_preprocessor_flags_for_build_mode() + [
"-DHERMES_BYTECODE_VERSION={}".format(HERMES_BYTECODE_VERSION),
] + rn_extra_build_flags(),
preprocessor_flags = get_objc_arc_preprocessor_flags() + get_preprocessor_flags_for_build_mode() + rn_extra_build_flags(),
visibility = [
"//fbobjc/Apps/Internal/SparkLabs/...",
"//fbobjc/Apps/Internal/Venice/...",
Expand Down
10 changes: 2 additions & 8 deletions packages/react-native/React/Base/RCTBundleURLProvider.mm
Original file line number Diff line number Diff line change
Expand Up @@ -265,19 +265,13 @@ + (NSURL *)jsBundleURLForBundleRoot:(NSString *)bundleRoot
runModule:(BOOL)runModule
{
NSString *path = [NSString stringWithFormat:@"/%@.bundle", bundleRoot];
#ifdef HERMES_BYTECODE_VERSION
NSString *runtimeBytecodeVersion = [NSString stringWithFormat:@"&runtimeBytecodeVersion=%u", HERMES_BYTECODE_VERSION];
#else
NSString *runtimeBytecodeVersion = @"";
#endif

// When we support only iOS 8 and above, use queryItems for a better API.
NSString *query = [NSString stringWithFormat:@"platform=ios&dev=%@&minify=%@&modulesOnly=%@&runModule=%@%@",
NSString *query = [NSString stringWithFormat:@"platform=ios&dev=%@&minify=%@&modulesOnly=%@&runModule=%@",
enableDev ? @"true" : @"false",
enableMinification ? @"true" : @"false",
modulesOnly ? @"true" : @"false",
runModule ? @"true" : @"false",
runtimeBytecodeVersion];
runModule ? @"true" : @"false"];

NSString *bundleID = [[NSBundle mainBundle] objectForInfoDictionaryKey:(NSString *)kCFBundleIdentifierKey];
if (bundleID) {
Expand Down
2 changes: 0 additions & 2 deletions packages/react-native/React/Base/RCTJavaScriptLoader.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@

extern NSString *const RCTJavaScriptLoaderErrorDomain;

extern const uint32_t RCT_BYTECODE_ALIGNMENT;

NS_ENUM(NSInteger){
RCTJavaScriptLoaderErrorNoScriptURL = 1,
RCTJavaScriptLoaderErrorFailedOpeningFile = 2,
Expand Down
13 changes: 2 additions & 11 deletions packages/react-native/React/Base/RCTJavaScriptLoader.mm
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@

NSString *const RCTJavaScriptLoaderErrorDomain = @"RCTJavaScriptLoaderErrorDomain";

const uint32_t RCT_BYTECODE_ALIGNMENT = 4;

@interface RCTSource () {
@public
NSURL *_url;
Expand All @@ -41,12 +39,7 @@ @implementation RCTSource

RCTSource *source = [RCTSource new];
source->_url = url;
// Multipart responses may give us an unaligned view into the buffer. This ensures memory is aligned.
if (parseTypeFromHeader(header) == ScriptTag::MetroHBCBundle && ((long)[data bytes] % RCT_BYTECODE_ALIGNMENT)) {
source->_data = [[NSData alloc] initWithData:data];
} else {
source->_data = data;
}
source->_data = data;
source->_length = length;
source->_filesChangedCount = RCTSourceFilesChangedCountNotBuiltByBundler;
return source;
Expand Down Expand Up @@ -171,7 +164,6 @@ + (NSData *)attemptSynchronousLoadOfBundleAtURL:(NSURL *)scriptURL

facebook::react::ScriptTag tag = facebook::react::parseTypeFromHeader(header);
switch (tag) {
case facebook::react::ScriptTag::MetroHBCBundle:
case facebook::react::ScriptTag::RAMBundle:
break;

Expand Down Expand Up @@ -285,8 +277,7 @@ static void attemptAsynchronousLoadOfBundleAtURL(
// Validate that the packager actually returned javascript.
NSString *contentType = headers[@"Content-Type"];
NSString *mimeType = [[contentType componentsSeparatedByString:@";"] firstObject];
if (![mimeType isEqualToString:@"application/javascript"] && ![mimeType isEqualToString:@"text/javascript"] &&
![mimeType isEqualToString:@"application/x-metro-bytecode-bundle"]) {
if (![mimeType isEqualToString:@"application/javascript"] && ![mimeType isEqualToString:@"text/javascript"]) {
NSString *description;
if ([mimeType isEqualToString:@"application/json"]) {
NSError *parseError;
Expand Down
15 changes: 1 addition & 14 deletions packages/react-native/React/CxxBridge/RCTCxxBridge.mm
Original file line number Diff line number Diff line change
Expand Up @@ -1494,11 +1494,6 @@ - (void)executeApplicationScriptSync:(NSData *)script url:(NSURL *)url
[self executeApplicationScript:script url:url async:NO];
}

static uint32_t RCTReadUInt32LE(NSData *script, uint32_t offset)
{
return [script length] < offset + 4 ? 0 : CFSwapInt32LittleToHost(*(((uint32_t *)[script bytes]) + offset / 4));
}

- (void)executeApplicationScript:(NSData *)script url:(NSURL *)url async:(BOOL)async
{
[self _tryAndHandleError:^{
Expand All @@ -1514,15 +1509,7 @@ - (void)executeApplicationScript:(NSData *)script url:(NSURL *)url async:(BOOL)a
// hold a local reference to reactInstance in case a parallel thread
// resets it between null check and usage
auto reactInstance = self->_reactInstance;
if (reactInstance && scriptType == ScriptTag::MetroHBCBundle) {
uint32_t offset = 8;
while (offset < script.length) {
uint32_t fileLength = RCTReadUInt32LE(script, offset);
NSData *unit = [script subdataWithRange:NSMakeRange(offset + 4, fileLength)];
reactInstance->loadScriptFromString(std::make_unique<NSDataBigString>(unit), sourceUrlStr.UTF8String, false);
offset += ((fileLength + RCT_BYTECODE_ALIGNMENT - 1) & ~(RCT_BYTECODE_ALIGNMENT - 1)) + 4;
}
} else if (scriptType == ScriptTag::RAMBundle) {
if (scriptType == ScriptTag::RAMBundle) {
[self->_performanceLogger markStartForTag:RCTPLRAMBundleLoad];
auto ramBundle = std::make_unique<JSIndexedRAMBundle>(sourceUrlStr.UTF8String);
std::unique_ptr<const JSBigString> scriptStr = ramBundle->getStartupCode();
Expand Down
1 change: 0 additions & 1 deletion packages/react-native/ReactAndroid/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,6 @@ android {

buildConfigField("boolean", "IS_INTERNAL_BUILD", "false")
buildConfigField("int", "EXOPACKAGE_FLAGS", "0")
buildConfigField("int", "HERMES_BYTECODE_VERSION", "0")

resValue "integer", "react_native_dev_server_port", reactNativeDevServerPort()
resValue "integer", "react_native_inspector_proxy_port", reactNativeInspectorProxyPort()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
load("//tools/build_defs/oss:rn_defs.bzl", "HERMES_BYTECODE_VERSION", "react_native_dep", "rn_android_build_config", "rn_android_library")
load("//tools/build_defs/oss:rn_defs.bzl", "react_native_dep", "rn_android_build_config", "rn_android_library")

SUB_PROJECTS = [
"network/**/*",
Expand Down Expand Up @@ -40,7 +40,6 @@ rn_android_build_config(
package = "com.facebook.react",
values = [
"boolean IS_INTERNAL_BUILD = true",
"int HERMES_BYTECODE_VERSION = {}".format(HERMES_BYTECODE_VERSION),
],
visibility = [
"PUBLIC",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,4 @@ public class ReactBuildConfig {
public static final boolean DEBUG = BuildConfig.DEBUG;
public static final boolean IS_INTERNAL_BUILD = BuildConfig.IS_INTERNAL_BUILD;
public static final int EXOPACKAGE_FLAGS = BuildConfig.EXOPACKAGE_FLAGS;
public static final int HERMES_BYTECODE_VERSION = BuildConfig.HERMES_BYTECODE_VERSION;
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import com.facebook.infer.annotation.Assertions;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.common.ReactConstants;
import com.facebook.react.common.build.ReactBuildConfig;
import com.facebook.react.devsupport.interfaces.DevBundleDownloadListener;
import com.facebook.react.devsupport.interfaces.PackagerStatusCallback;
import com.facebook.react.devsupport.interfaces.StackFrame;
Expand Down Expand Up @@ -427,22 +426,17 @@ private String createSplitBundleURL(String mainModuleID, String host) {

private String createBundleURL(
String mainModuleID, BundleType type, String host, boolean modulesOnly, boolean runModule) {
String runtimeBytecodeVersion =
ReactBuildConfig.HERMES_BYTECODE_VERSION != 0
? "&runtimeBytecodeVersion=" + ReactBuildConfig.HERMES_BYTECODE_VERSION
: "";
return String.format(
Locale.US,
"http://%s/%s.%s?platform=android&dev=%s&minify=%s&app=%s&modulesOnly=%s&runModule=%s%s",
"http://%s/%s.%s?platform=android&dev=%s&minify=%s&app=%s&modulesOnly=%s&runModule=%s",
host,
mainModuleID,
type.typeID(),
getDevMode(),
getJSMinifyMode(),
mPackageName,
modulesOnly ? "true" : "false",
runModule ? "true" : "false",
runtimeBytecodeVersion);
runModule ? "true" : "false");
}

private String createBundleURL(String mainModuleID, BundleType type) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -290,30 +290,6 @@ void CatalystInstanceImpl::jniLoadScriptFromFile(
}

switch (getScriptTagFromFile(fileName.c_str())) {
case ScriptTag::MetroHBCBundle: {
std::unique_ptr<const JSBigFileString> script;
RecoverableError::runRethrowingAsRecoverable<std::system_error>(
[&fileName, &script]() {
script = JSBigFileString::fromPath(fileName);
});
const char *buffer = script->c_str();
uint32_t bufferLength = (uint32_t)script->size();
uint32_t offset = 8;
while (offset < bufferLength) {
uint32_t segment = offset + 4;
uint32_t moduleLength =
bufferLength < segment ? 0 : *(((uint32_t *)buffer) + offset / 4);

reactInstance->loadScriptFromString(
std::make_unique<const JSBigStdString>(
std::string(buffer + segment, buffer + moduleLength + segment)),
sourceURL,
false);

offset += ((moduleLength + 3) & ~3) + 4;
}
break;
}
case ScriptTag::RAMBundle:
instance_->loadRAMBundleFromFile(fileName, sourceURL, loadSynchronously);
break;
Expand Down
5 changes: 0 additions & 5 deletions packages/react-native/ReactCommon/cxxreact/JSBundleType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ namespace facebook {
namespace react {

static uint32_t constexpr RAMBundleMagicNumber = 0xFB0BD1E5;
static uint32_t constexpr MetroHBCBundleMagicNumber = 0xFFE7C3C3;

// "Hermes" in ancient Greek encoded in UTF-16BE and truncated to 8 bytes.
static uint64_t constexpr HermesBCBundleMagicNumber = 0x1F1903C103BC1FC6;
Expand All @@ -20,8 +19,6 @@ ScriptTag parseTypeFromHeader(const BundleHeader &header) {
switch (header.magic32.value) {
case RAMBundleMagicNumber:
return ScriptTag::RAMBundle;
case MetroHBCBundleMagicNumber:
return ScriptTag::MetroHBCBundle;
default:
return ScriptTag::String;
}
Expand All @@ -33,8 +30,6 @@ const char *stringForScriptTag(const ScriptTag &tag) {
return "String";
case ScriptTag::RAMBundle:
return "RAM Bundle";
case ScriptTag::MetroHBCBundle:
return "Metro Hermes Bytecode Bundle";
}
return "";
}
Expand Down
1 change: 0 additions & 1 deletion packages/react-native/ReactCommon/cxxreact/JSBundleType.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ namespace react {
enum struct ScriptTag {
String = 0,
RAMBundle,
MetroHBCBundle,
};

/**
Expand Down
5 changes: 1 addition & 4 deletions packages/rn-tester/BUCK
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
load("@fbsource//tools/build_defs:glob_defs.bzl", "subdir_glob")
load("@fbsource//xplat/hermes/defs:hermes.bzl", "HERMES_BYTECODE_VERSION")
load("//tools/build_defs:fb_native_wrapper.bzl", "fb_native")
load("//tools/build_defs:fb_xplat_platform_specific_rule.bzl", "fb_xplat_platform_specific_rule")
load("//tools/build_defs:fb_xplat_resource.bzl", "fb_xplat_resource")
Expand Down Expand Up @@ -138,9 +137,7 @@ fb_apple_test(
"QuartzCore",
"UIKit",
],
preprocessor_flags = get_objc_arc_preprocessor_flags() + [
"-DHERMES_BYTECODE_VERSION={}".format(HERMES_BYTECODE_VERSION),
] + get_preprocessor_flags_for_build_mode(),
preprocessor_flags = get_objc_arc_preprocessor_flags() + get_preprocessor_flags_for_build_mode(),
visibility = [
"//fbobjc/Libraries/FBReactKit:workspace",
],
Expand Down
20 changes: 0 additions & 20 deletions packages/rn-tester/RNTesterUnitTests/RCTBundleURLProviderTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -22,42 +22,22 @@

static NSURL *localhostBundleURL()
{
#ifdef HERMES_BYTECODE_VERSION
return [NSURL
URLWithString:
[NSString
stringWithFormat:
@"http://localhost:8081/%@.bundle?platform=ios&dev=true&minify=false&modulesOnly=false&runModule=true&runtimeBytecodeVersion=%u&app=com.apple.dt.xctest.tool",
testFile,
HERMES_BYTECODE_VERSION]];
#else
return [NSURL
URLWithString:
[NSString
stringWithFormat:
@"http://localhost:8081/%@.bundle?platform=ios&dev=true&minify=false&modulesOnly=false&runModule=true&app=com.apple.dt.xctest.tool",
testFile]];
#endif
}

static NSURL *ipBundleURL()
{
#ifdef HERMES_BYTECODE_VERSION
return [NSURL
URLWithString:
[NSString
stringWithFormat:
@"http://192.168.1.1:8081/%@.bundle?platform=ios&dev=true&minify=false&modulesOnly=false&runModule=true&runtimeBytecodeVersion=%u&app=com.apple.dt.xctest.tool",
testFile,
HERMES_BYTECODE_VERSION]];
#else
return [NSURL
URLWithString:
[NSString
stringWithFormat:
@"http://192.168.1.1:8081/%@.bundle?platform=ios&dev=true&minify=false&modulesOnly=false&runModule=true&app=com.apple.dt.xctest.tool",
testFile]];
#endif
}

@implementation NSBundle (RCTBundleURLProviderTests)
Expand Down
2 changes: 0 additions & 2 deletions tools/build_defs/oss/rn_defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -428,8 +428,6 @@ def react_fabric_component_plugin_provider(name, native_class_func):
def react_cxx_module_plugin_provider(name, function):
return None

HERMES_BYTECODE_VERSION = -1

RCT_IMAGE_DATA_DECODER_SOCKET = None
RCT_IMAGE_URL_LOADER_SOCKET = None
RCT_URL_REQUEST_HANDLER_SOCKET = None
Expand Down

0 comments on commit 6abc097

Please sign in to comment.