Skip to content

Commit

Permalink
Indicate presence of CommonJS modules via getRuntimeProperties
Browse files Browse the repository at this point in the history
Summary: Adds a property to the object returned from `getRuntimeProperties` describing the current state of CommonJS modules in the runtime. This is done by linearly scanning all `RuntimeModules` and checking whether they contain any CommonJS modules.

Reviewed By: avp

Differential Revision: D24486526

fbshipit-source-id: febc78170be2c7b673ebbeb3f0f5d1e8c2f95d8d
  • Loading branch information
motiz88 authored and facebook-github-bot committed Oct 28, 2020
1 parent 60b8744 commit f3e86b4
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions lib/VM/JSLib/HermesInternal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,32 @@ hermesInternalGetInstrumentedStats(void *, Runtime *runtime, NativeArgs args) {
#undef SET_PROP_NEW
}

/// \return a static string summarising the presence and resolution type of
/// CommonJS modules across all RuntimeModules that have been loaded into \c
/// runtime.
static const char *getCJSModuleModeDescription(Runtime *runtime) {
bool hasCJSModulesDynamic = false;
bool hasCJSModulesStatic = false;
for (const auto &runtimeModule : runtime->getRuntimeModules()) {
if (runtimeModule.hasCJSModules()) {
hasCJSModulesDynamic = true;
}
if (runtimeModule.hasCJSModulesStatic()) {
hasCJSModulesStatic = true;
}
}
if (hasCJSModulesDynamic && hasCJSModulesStatic) {
return "Mixed dynamic/static";
}
if (hasCJSModulesDynamic) {
return "Dynamically resolved";
}
if (hasCJSModulesStatic) {
return "Statically resolved";
}
return "None";
}

/// \return an object mapping keys to runtime property values.
CallResult<HermesValue>
hermesInternalGetRuntimeProperties(void *, Runtime *runtime, NativeArgs args) {
Expand Down Expand Up @@ -506,6 +532,19 @@ hermesInternalGetRuntimeProperties(void *, Runtime *runtime, NativeArgs args) {
}
#endif

const char *cjsModuleMode = getCJSModuleModeDescription(runtime);
auto cjsModuleModeRes =
StringPrimitive::create(runtime, createASCIIRef(cjsModuleMode));
if (LLVM_UNLIKELY(cjsModuleModeRes == ExecutionStatus::EXCEPTION)) {
return ExecutionStatus::EXCEPTION;
}
tmpHandle = *cjsModuleModeRes;
if (LLVM_UNLIKELY(
addProperty(tmpHandle, "CommonJS Modules") ==
ExecutionStatus::EXCEPTION)) {
return ExecutionStatus::EXCEPTION;
}

return resultHandle.getHermesValue();
}

Expand Down

0 comments on commit f3e86b4

Please sign in to comment.