Skip to content

Commit

Permalink
fix: fix wbc.
Browse files Browse the repository at this point in the history
  • Loading branch information
andycall committed Apr 11, 2024
1 parent 451286b commit 6cbcfaa
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 11 deletions.
16 changes: 14 additions & 2 deletions bridge/core/api/api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -72,18 +72,23 @@ void evaluateQuickjsByteCodeInternal(void* page_,

void evaluateWbcInternal(void* page_,
uint8_t* bytes,
int32_t byteLen,
int32_t byte_len,
int64_t profile_id,
Dart_PersistentHandle persistent_handle,
EvaluateQuickjsByteCodeCallback result_callback) {
auto page = reinterpret_cast<webf::WebFPage*>(page_);
assert(std::this_thread::get_id() == page->currentThread());

page->dartIsolateContext()->profiler()->StartTrackEvaluation(profile_id);

size_t dataBlockSize;
bool is_success;
webf::Wbc wbc = webf::Wbc();
uint8_t* dataBlockBytes = wbc.prepareWbc(bytes, byteLen, &dataBlockSize);
uint8_t* dataBlockBytes = wbc.prepareWbc(bytes, byte_len, &dataBlockSize);
if (dataBlockBytes == nullptr) {
#if ENABLE_LOG
WEBF_LOG(ERROR) << "prepareWbc error" << std::endl;
#endif
is_success = false;
} else {
std::vector<char> decompressedBytes;
Expand All @@ -95,14 +100,21 @@ void evaluateWbcInternal(void* page_,
dataBlockBytes = NULL;

if (decompressedSize < 0) {
#if ENABLE_LOG
WEBF_LOG(ERROR) << "LZ4 decompression failed with error code: " << decompressedSize << std::endl;
#endif
is_success = false;
} else {
#if ENABLE_LOG
WEBF_LOG(VERBOSE) << "LZ4 decompression success! " << decompressedSize << std::endl;
#endif
is_success = page->evaluateByteCode(reinterpret_cast<uint8_t*>(decompressedBytes.data()), decompressedSize);
WEBF_LOG(VERBOSE) << " SUCCESS: " << is_success;
}
}

page->dartIsolateContext()->profiler()->FinishTrackEvaluation(profile_id);

page->dartIsolateContext()->dispatcher()->PostToDart(page->isDedicated(), ReturnEvaluateQuickjsByteCodeResultToDart,
persistent_handle, result_callback, is_success);
}
Expand Down
3 changes: 2 additions & 1 deletion bridge/core/api/api.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ void evaluateQuickjsByteCodeInternal(void* page_,

void evaluateWbcInternal(void* page_,
uint8_t* bytes,
int32_t byteLen,
int32_t byte_len,
int64_t profile_id,
Dart_PersistentHandle persistent_handle,
EvaluateQuickjsByteCodeCallback result_callback);
void parseHTMLInternal(void* page_,
Expand Down
8 changes: 8 additions & 0 deletions bridge/include/webf_bridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,14 @@ void dumpQuickjsByteCode(void* page,
DumpQuickjsByteCodeCallback result_callback);

WEBF_EXPORT_C
void evaluateWbc(void* page_,
uint8_t* bytes,
int32_t byte_len,
int64_t profile_id,
Dart_Handle dart_handle,
EvaluateQuickjsByteCodeCallback result_callback);

WEBF_EXPORT_C
void parseHTML(void* page,
char* code,
int32_t length,
Expand Down
5 changes: 3 additions & 2 deletions bridge/webf_bridge.cc
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,8 @@ void parseHTML(void* page_,

void evaluateWbc(void* page_,
uint8_t* bytes,
int32_t byteLen,
int32_t byte_len,
int64_t profile_id,
Dart_Handle dart_handle,
EvaluateQuickjsByteCodeCallback result_callback) {
#if ENABLE_LOG
Expand All @@ -214,7 +215,7 @@ void evaluateWbc(void* page_,
auto page = reinterpret_cast<webf::WebFPage*>(page_);
Dart_PersistentHandle persistent_handle = Dart_NewPersistentHandle_DL(dart_handle);
page->dartIsolateContext()->dispatcher()->PostToJs(page->isDedicated(), page->contextId(),
webf::evaluateWbcInternal, page_, bytes, byteLen,
webf::evaluateWbcInternal, page_, bytes, byte_len, profile_id,
persistent_handle, result_callback);
}

Expand Down
2 changes: 1 addition & 1 deletion webf/example/assets/bundle.html
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,6 @@
<script>
console.log('Welcome to webf!');
</script>
<script src="./bundle.js"></script>
<script src="./bundle.wbc1"></script>
</body>
</html>
Binary file added webf/example/assets/bundle.wbc1
Binary file not shown.
4 changes: 2 additions & 2 deletions webf/lib/src/bridge/to_native.dart
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ Future<bool> evaluateQuickjsByteCode(double contextId, Uint8List bytes, { Evalua
return completer.future;
}

Future<bool> evaluateWbc(double contextId, Uint8List bytes) async {
Future<bool> evaluateWbc(double contextId, Uint8List bytes, { EvaluateOpItem? profileOp }) async {
if (WebFController.getControllerOfJSContextId(contextId) == null) {
return false;
}
Expand All @@ -403,7 +403,7 @@ Future<bool> evaluateWbc(double contextId, Uint8List bytes) async {
Pointer<NativeFunction<NativeEvaluateQuickjsByteCodeCallback>> nativeCallback =
Pointer.fromFunction(handleEvaluateQuickjsByteCodeResult);

_evaluateWbc(_allocatedPages[contextId]!, byteData, bytes.length, context, nativeCallback);
_evaluateWbc(_allocatedPages[contextId]!, byteData, bytes.length, profileOp?.hashCode ?? 0, context, nativeCallback);

return completer.future;
}
Expand Down
7 changes: 4 additions & 3 deletions webf/lib/src/html/script.dart
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,12 @@ class ScriptRunner {
}
} else if (bundle.isBytecode) {
bool result = false;
if (bundle.url.contains('.kbc1')) {
result = await evaluateQuickjsByteCode(contextId, bundle.data!, profileOp: profileOp);
} else if (bundle.url.contains('.wbc1')) {
if (bundle.url.contains('.wbc1')) {
result = await evaluateWbc(contextId, bundle.data!, profileOp: profileOp);
} else {
result = await evaluateQuickjsByteCode(contextId, bundle.data!, profileOp: profileOp);
}
print('result: $result');

if (!result) {
throw FlutterError('Bytecode are not valid to execute.');
Expand Down

0 comments on commit 6cbcfaa

Please sign in to comment.