Skip to content

Commit 960c79c

Browse files
committed
fixup! module: integrate TypeScript into compile cache
1 parent 0e02024 commit 960c79c

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

lib/internal/modules/typescript.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,9 @@ function stripTypeScriptTypes(code, options = kEmptyObject) {
9393
}
9494

9595
/**
96+
* @typedef {'strip-only' | 'transform'} TypeScriptMode
9697
* @typedef {object} TypeScriptOptions
97-
* @property {'transform'|'strip-only'} mode Mode.
98+
* @property {TypeScriptMode} mode Mode.
9899
* @property {boolean} sourceMap Whether to generate source maps.
99100
* @property {string|undefined} filename Filename.
100101
*/
@@ -122,7 +123,7 @@ function processTypeScriptCode(code, options) {
122123

123124
/**
124125
* Get the type enum used for compile cache.
125-
* @param {'strip-only'|'transform'} mode Mode of transpilation.
126+
* @param {TypeScriptMode} mode Mode of transpilation.
126127
* @param {boolean} sourceMap Whether source maps are enabled.
127128
* @returns {number}
128129
*/
@@ -158,6 +159,11 @@ function stripTypeScriptModuleTypes(source, filename, emitWarning = true) {
158159
// as checking process.env equally involves calling into C++ anyway, and
159160
// the compile cache can be enabled dynamically.
160161
const type = getCachedCodeType(mode, sourceMap);
162+
// Get a compile cache entry into the native compile cache store,
163+
// keyed by the filename. If the cache can already be loaded on disk,
164+
// cached.transpiled contains the cached string. Otherwise we should do
165+
// the transpilation and save it in the native store later using
166+
// saveCompileCacheEntry().
161167
const cached = (filename ? getCompileCacheEntry(source, filename, type) : undefined);
162168
if (cached?.transpiled) { // TODO(joyeecheung): return Buffer here.
163169
return cached.transpiled;
@@ -171,6 +177,11 @@ function stripTypeScriptModuleTypes(source, filename, emitWarning = true) {
171177

172178
const transpiled = processTypeScriptCode(source, options);
173179
if (cached) {
180+
// cached.external contains a pointer to the native cache entry.
181+
// The cached object would be unreachable once it's out of scope,
182+
// but the pointer inside cached.external would stay around for reuse until
183+
// environment shutdown or when the cache is manually flushed
184+
// to disk. Unwrap it in JS before passing into C++ since it's faster.
174185
saveCompileCacheEntry(cached.external, transpiled);
175186
}
176187
return transpiled;

src/node_modules.cc

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,13 @@ using v8::Array;
2424
using v8::Context;
2525
using v8::FunctionCallbackInfo;
2626
using v8::HandleScope;
27+
using v8::Integer;
2728
using v8::Isolate;
2829
using v8::Local;
2930
using v8::LocalVector;
31+
using v8::Name;
3032
using v8::NewStringType;
33+
using v8::Null;
3134
using v8::Object;
3235
using v8::ObjectTemplate;
3336
using v8::Primitive;
@@ -609,20 +612,20 @@ void BindingData::CreatePerContextProperties(Local<Object> target,
609612
compile_cache_status_values.data(),
610613
compile_cache_status_values.size())));
611614

612-
LocalVector<v8::Name> cached_code_type_keys(isolate);
615+
LocalVector<Name> cached_code_type_keys(isolate);
613616
LocalVector<Value> cached_code_type_values(isolate);
614617

615618
#define V(type, value) \
616619
cached_code_type_keys.push_back(FIXED_ONE_BYTE_STRING(isolate, #type)); \
617-
cached_code_type_values.push_back(v8::Integer::New(isolate, value)); \
620+
cached_code_type_values.push_back(Integer::New(isolate, value)); \
618621
DCHECK_EQ(value, cached_code_type_values.size() - 1);
619622
CACHED_CODE_TYPES(V)
620623
#undef V
621624

622625
USE(target->Set(context,
623626
FIXED_ONE_BYTE_STRING(isolate, "cachedCodeTypes"),
624627
Object::New(isolate,
625-
v8::Null(isolate),
628+
Null(isolate),
626629
cached_code_type_keys.data(),
627630
cached_code_type_values.data(),
628631
cached_code_type_keys.size())));

0 commit comments

Comments
 (0)