Skip to content

Commit

Permalink
implement Function.toString()
Browse files Browse the repository at this point in the history
  • Loading branch information
Juby210 committed Apr 19, 2022
1 parent e7e39da commit b4b8f25
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
7 changes: 6 additions & 1 deletion .github/workflows/ci.yml
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@

name: CI

on: [push, pull_request]
on:
- push:
branches:
- 0.11.0-aliucord

- pull_request

jobs:
build-android:
Expand Down
27 changes: 23 additions & 4 deletions lib/VM/JSLib/Function.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
//===----------------------------------------------------------------------===//
#include "JSLibInternal.h"

#include "hermes/BCGen/HBC/BytecodeDisassembler.h"
#include "hermes/Regex/Executor.h"
#include "hermes/Regex/RegexTraits.h"
#include "hermes/VM/ArrayLike.h"
#include "hermes/VM/Callable.h"
#include "hermes/VM/Operations.h"
Expand Down Expand Up @@ -191,9 +191,28 @@ functionPrototypeToString(void *, Runtime *runtime, NativeArgs args) {
strBuf.append(buf);
}

// Avoid using the [native code] string to prevent extra wrapping overhead
// in, e.g., Babel's class extension mechanism.
strBuf.append(") { [bytecode] }");
strBuf.append(") {\n");

if (auto jsFunc = dyn_vmcast<JSFunction>(*func)) {
auto funcId = jsFunc->getCodeBlock()->getFunctionID();

hbc::BytecodeDisassembler disassembler(jsFunc->getRuntimeModule()->getBytecodeSharedPtr());
hbc::DisassemblyOptions options = hbc::DisassemblyOptions::IncludeSource |
hbc::DisassemblyOptions::IncludeFunctionIds | hbc::DisassemblyOptions::Pretty;
disassembler.setOptions(options);

std::string str;
llvh::raw_string_ostream output(str);
disassembler.disassembleFunction(funcId, output);

strBuf.append(output.str());
} else {
// Avoid using the [native code] string to prevent extra wrapping overhead
// in, e.g., Babel's class extension mechanism.
strBuf.append(" [bytecode]\n");
}

strBuf.append("}");
}

// Finally allocate a StringPrimitive.
Expand Down

0 comments on commit b4b8f25

Please sign in to comment.