Skip to content

Commit 4dc19b6

Browse files
joshualittCommit Queue
authored and
Commit Queue
committed
[dart2js] Ensure JavaScriptFunction gets emitted for allowInterop
Change-Id: I320653a46da31cfb626c41a3a2307b1d1626173a Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/280205 Reviewed-by: Stephen Adams <sra@google.com> Commit-Queue: Joshua Litt <joshualitt@google.com>
1 parent 09c9301 commit 4dc19b6

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

pkg/compiler/lib/src/js_emitter/interceptor_stub_generator.dart

+13
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ class InterceptorStubGenerator {
7474
condition = js('(typeof receiver) == "string"');
7575
} else if (cls == _commonElements.jsNullClass) {
7676
condition = js('receiver == null');
77+
} else if (cls == _commonElements.jsJavaScriptFunctionClass) {
78+
condition = js('(typeof receiver) == "function"');
7779
} else {
7880
throw 'internal error';
7981
}
@@ -89,6 +91,7 @@ class InterceptorStubGenerator {
8991
bool hasString = false;
9092
bool hasNative = false;
9193
bool anyNativeClasses = _nativeCodegenEnqueuer.hasInstantiatedNativeClasses;
94+
bool hasJavaScriptFunction = false;
9295

9396
for (ClassEntity cls in classes) {
9497
if (cls == _commonElements.jsArrayClass ||
@@ -108,6 +111,8 @@ class InterceptorStubGenerator {
108111
hasNumber = true;
109112
else if (cls == _commonElements.jsStringClass)
110113
hasString = true;
114+
else if (cls == _commonElements.jsJavaScriptFunctionClass)
115+
hasJavaScriptFunction = true;
111116
else {
112117
// The set of classes includes classes mixed-in to interceptor classes
113118
// and user extensions of native classes.
@@ -180,6 +185,14 @@ class InterceptorStubGenerator {
180185
statements.add(buildInterceptorCheck(_commonElements.jsArrayClass));
181186
}
182187

188+
// If a program `hasNative` then we will insert a check for
189+
// `JavaScriptFunction` in the `hasNative` block of the interceptor logic.
190+
// Otherwise, we have to insert a specific check for `JavScriptFunction.
191+
if (hasJavaScriptFunction && !hasNative) {
192+
statements.add(
193+
buildInterceptorCheck(_commonElements.jsJavaScriptFunctionClass));
194+
}
195+
183196
if (hasNative) {
184197
statements.add(js.statement(r'''{
185198
if (typeof receiver != "object") {

pkg/compiler/lib/src/js_emitter/program_builder/program_builder.dart

+3-1
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,9 @@ class ProgramBuilder {
251251
_nativeCodegenEnqueuer.hasInstantiatedNativeClasses ||
252252
_nativeData.isAllowInteropUsed;
253253

254-
assert(!needsNativeSupport || nativeClasses.isNotEmpty);
254+
assert(!needsNativeSupport ||
255+
nativeClasses.isNotEmpty ||
256+
_nativeData.isAllowInteropUsed);
255257

256258
List<js.TokenFinalizer> finalizers = [_task.metadataCollector];
257259
if (_namer is js.TokenFinalizer) {

pkg/compiler/lib/src/native/enqueue.dart

+1
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ abstract class NativeEnqueuer {
9292
type == _commonElements.stringType ||
9393
type == _commonElements.nullType ||
9494
type == _commonElements.boolType ||
95+
type == _commonElements.jsJavaScriptFunctionType ||
9596
_dartTypes.isSubtype(type,
9697
_elementEnvironment.getRawType(_commonElements.jsArrayClass))) {
9798
registerInstantiation(type);

0 commit comments

Comments
 (0)