Skip to content

Commit

Permalink
AliuHermes 0.11.0: init
Browse files Browse the repository at this point in the history
  • Loading branch information
Juby210 committed Apr 19, 2022
1 parent 2040453 commit e7e39da
Show file tree
Hide file tree
Showing 7 changed files with 172 additions and 1 deletion.
38 changes: 38 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@

name: CI

on: [push, pull_request]

jobs:
build-android:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2

- name: Setup
run: |
sudo apt-get update
sudo apt-get install -y ninja-build
- name: ccache
uses: hendrikmuhs/ccache-action@4bcd0c14065d24d92f5e937fae51361c64048b46
with:
key: android

- name: Build compiler
run: |
utils/build/configure.py ./build
cmake --build ./build --target hermesc
- name: Build hermes for android
run: |
cd android
HERMES_WS_DIR=.. ./gradlew cppruntime:assembleRelease hermes:assembleIntlRelease
- uses: actions/upload-artifact@v3
with:
name: android
path: |
build_android/outputs/hermes-cppruntime-release.aar
build_android/outputs/hermes-release.aar
42 changes: 41 additions & 1 deletion API/hermes/hermes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1409,7 +1409,47 @@ jsi::Value HermesRuntimeImpl::evaluatePreparedJavaScript(
jsi::Value HermesRuntimeImpl::evaluateJavaScript(
const std::shared_ptr<const jsi::Buffer> &buffer,
const std::string &sourceURL) {
return evaluateJavaScriptWithSourceMap(buffer, nullptr, sourceURL);
auto isMainBundle = sourceURL == "index.android.bundle" ||
sourceURL == "discord.android.bundle" ||
sourceURL.find("main.jsbundle") != std::string::npos;

if (isMainBundle) {
::hermes::hermesLog("AliuHermes", "Injecting modulesPatch");
evaluateJavaScript(
std::make_unique<jsi::StringBuffer>(std::string(
"const oldObjectCreate = this.Object.create;"
"const _window = this;"
"_window.Object.create = (...args) => {"
" const obj = oldObjectCreate.apply(_window.Object, args);"
" if (args[0] === null) {"
" _window.modules = obj;"
" _window.Object.create = oldObjectCreate;"
" }"
" return obj;"
"};"
)),
"modulesPatch"
);
}

auto returnValue =
evaluatePreparedJavaScript(prepareJavaScript(buffer, sourceURL));

if (isMainBundle) {
::hermes::hermesLog("AliuHermes", "Injecting bootstrap");
evaluateJavaScript(
std::make_unique<jsi::StringBuffer>(std::string(
"fetch('http://localhost:3000/Aliucord.js').catch(console.error).then(x => x.text()).then(code => {"
" fetch('http://localhost:3000/Aliucord.js.map')"
" .catch(e => { console.error(e); (0, eval)(code) })"
" .then(x => x.text()).then(sourceMap => (0, HermesInternal.aliuEval)(code, sourceMap))"
"})"
)),
"bootstrap"
);
}

return returnValue;
}

bool HermesRuntimeImpl::drainMicrotasks(int maxMicrotasksHint) {
Expand Down
1 change: 1 addition & 0 deletions android/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.gradle
.externalNativeBuild
staging
2 changes: 2 additions & 0 deletions include/hermes/VM/NativeFunctions.def
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@ NATIVE_FUNCTION(hermesInternalUseEngineQueue)
NATIVE_FUNCTION(hermesInternalEnqueueJob)
NATIVE_FUNCTION(hermesInternalDrainJobs)

NATIVE_FUNCTION(hermesInternalAliuEval)

#ifdef HERMES_ENABLE_DEBUGGER
NATIVE_FUNCTION(isDebuggerAttached)
NATIVE_FUNCTION(shouldPauseOnThrow)
Expand Down
1 change: 1 addition & 0 deletions include/hermes/VM/PredefinedStrings.def
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,7 @@ STR(fileName, "fileName")
STR(setPromiseRejectionTrackingHook, "setPromiseRejectionTrackingHook")
STR(enablePromiseRejectionTracker, "enablePromiseRejectionTracker")
STR(spawnAsync, "spawnAsync") /* NOLINT */
STR(aliuEval, "aliuEval")

STR(require, "require")
STR(requireFast, "requireFast")
Expand Down
87 changes: 87 additions & 0 deletions lib/VM/JSLib/HermesInternal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@

#include "JSLibInternal.h"

#include "hermes/BCGen/HBC/HBC.h"
#include "hermes/BCGen/HBC/BytecodeFileFormat.h"
#include "hermes/SourceMap/SourceMapParser.h"
#include "hermes/Support/Base64vlq.h"
#include "hermes/Support/MemoryBuffer.h"
#include "hermes/Support/OSCompat.h"
#include "hermes/Support/SimpleDiagHandler.h"
#include "hermes/VM/Callable.h"
#include "hermes/VM/JSArray.h"
#include "hermes/VM/JSArrayBuffer.h"
Expand Down Expand Up @@ -817,6 +821,87 @@ CallResult<HermesValue> hermesInternalEnablePromiseRejectionTracker(
.toCallResultHermesValue();
}

// AliuHermes - HermesInternal.aliuEval(code: string, sourceMap?: string)
CallResult<HermesValue> hermesInternalAliuEval(
void *,
Runtime *runtime,
NativeArgs args) {
if (!args.getArg(0).isString()) {
return args.getArg(0);
}

auto str = args.dyncastArg<StringPrimitive>(0);
std::string code;
auto view = StringPrimitive::createStringView(runtime, str);
if (view.isASCII()) {
code = std::string(view.begin(), view.end());
} else {
SmallU16String<4> allocator;
convertUTF16ToUTF8WithReplacements(code, view.getUTF16Ref(allocator));
}

std::unique_ptr<::hermes::SourceMap> sourceMap{};
if (args.getArgCount() > 1 && args.getArg(1).isString()) {
auto sourceMapStr = args.dyncastArg<StringPrimitive>(1);
std::string sourceMapString;
auto sourceMapView = StringPrimitive::createStringView(runtime, sourceMapStr);
if (sourceMapView.isASCII()) {
sourceMapString = std::string(sourceMapView.begin(), sourceMapView.end());
} else {
SmallU16String<4> allocator;
convertUTF16ToUTF8WithReplacements(sourceMapString, sourceMapView.getUTF16Ref(allocator));
}

llvh::MemoryBufferRef mbref(
llvh::StringRef(
sourceMapString.data(), sourceMapString.size()),
"");
::hermes::SimpleDiagHandler diag;
::hermes::SourceErrorManager sm;
diag.installInto(sm);
sourceMap = ::hermes::SourceMapParser::parse(mbref, sm);
if (!sourceMap) {
auto errorStr = diag.getErrorString();
return runtime->raiseSyntaxError(TwineChar16(errorStr));
}
}

hbc::CompileFlags compileFlags;
compileFlags.includeLibHermes = false;
compileFlags.optimize = runtime->optimizedEval;
compileFlags.verifyIR = runtime->verifyEvalIR;
compileFlags.lazy =
code.size() >= compileFlags.preemptiveFileCompilationThreshold;

ScopeChain scopeChain{};
std::unique_ptr<hbc::BCProviderFromSrc> bytecode;
{
std::unique_ptr<hermes::Buffer> buffer;
if (compileFlags.lazy) {
buffer.reset(new hermes::OwnedMemoryBuffer(
llvh::MemoryBuffer::getMemBufferCopy(code)));
} else {
buffer.reset(new hermes::OwnedMemoryBuffer(
llvh::MemoryBuffer::getMemBuffer(code)));
}

auto bytecode_err = hbc::BCProviderFromSrc::createBCProviderFromSrc(
std::move(buffer), "aliuEval", std::move(sourceMap), compileFlags, scopeChain);
if (!bytecode_err.first) {
return runtime->raiseSyntaxError(TwineChar16(bytecode_err.second));
}
bytecode = std::move(bytecode_err.first);
}

llvh::StringRef sourceURL{};
return runtime->runBytecode(
std::move(bytecode),
RuntimeModuleFlags{},
sourceURL,
Runtime::makeNullHandle<Environment>(),
runtime->getGlobal());
}

Handle<JSObject> createHermesInternalObject(
Runtime *runtime,
const JSLibFlags &flags) {
Expand Down Expand Up @@ -913,6 +998,8 @@ Handle<JSObject> createHermesInternalObject(
defineInternMethod(P::ttrcReached, hermesInternalTTRCReached);
defineInternMethod(P::getFunctionLocation, hermesInternalGetFunctionLocation);

defineInternMethod(P::aliuEval, hermesInternalAliuEval);

// HermesInternal function that are only meant to be used for testing purpose.
// They can change language semantics and are security risks.
if (flags.enableHermesInternalTestMethods) {
Expand Down
2 changes: 2 additions & 0 deletions utils/build/configure.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,8 @@ def main():
args.cmake_flags.split()
+ python_executable_flag()
+ ["-DCMAKE_BUILD_TYPE=" + args.build_type]
+ ["-DCMAKE_C_COMPILER_LAUNCHER=ccache"]
+ ["-DCMAKE_CXX_COMPILER_LAUNCHER=ccache"]
)

if (
Expand Down

0 comments on commit e7e39da

Please sign in to comment.