@@ -28,6 +28,7 @@ import org.scalajs.linker.interface.unstable.RuntimeClassNameMapperImpl
28
28
import org .scalajs .linker .backend .javascript .Trees ._
29
29
30
30
import EmitterNames ._
31
+ import PolyfillableBuiltin ._
31
32
32
33
private [emitter] object CoreJSLib {
33
34
@@ -167,8 +168,8 @@ private[emitter] object CoreJSLib {
167
168
}
168
169
169
170
private def defineJSBuiltinsSnapshotsAndPolyfills (): Tree = {
170
- def genPolyfillFor (builtinName : String ): Tree = builtinName match {
171
- case " is " =>
171
+ def genPolyfillFor (builtin : PolyfillableBuiltin ): Tree = builtin match {
172
+ case ObjectIsBuiltin =>
172
173
val x = varRef(" x" )
173
174
val y = varRef(" y" )
174
175
genArrowFunction(paramList(x, y), Return {
@@ -181,7 +182,7 @@ private[emitter] object CoreJSLib {
181
182
})
182
183
})
183
184
184
- case " imul " =>
185
+ case ImulBuiltin =>
185
186
val a = varRef(" a" )
186
187
val b = varRef(" b" )
187
188
val ah = varRef(" ah" )
@@ -196,7 +197,7 @@ private[emitter] object CoreJSLib {
196
197
Return ((al * bl) + (((ah * bl + al * bh) << 16 ) >>> 0 ) | 0 )
197
198
))
198
199
199
- case " fround " =>
200
+ case FroundBuiltin =>
200
201
val v = varRef(" v" )
201
202
if (! strictFloats) {
202
203
genArrowFunction(paramList(v), Return (+ v))
@@ -300,7 +301,7 @@ private[emitter] object CoreJSLib {
300
301
typedArrayPolyfill, noTypedArrayPolyfill)
301
302
}
302
303
303
- case " clz32 " =>
304
+ case Clz32Builtin =>
304
305
val i = varRef(" i" )
305
306
val r = varRef(" r" )
306
307
genArrowFunction(paramList(i), Block (
@@ -314,7 +315,7 @@ private[emitter] object CoreJSLib {
314
315
Return (r + (i >> 31 ))
315
316
))
316
317
317
- case " privateJSFieldSymbol " =>
318
+ case PrivateSymbolBuiltin =>
318
319
/* function privateJSFieldSymbol(description) {
319
320
* function rand32() {
320
321
* const s = ((Math.random() * 4294967296.0) | 0).toString(16);
@@ -359,7 +360,7 @@ private[emitter] object CoreJSLib {
359
360
}
360
361
))
361
362
362
- case " getOwnPropertyDescriptors " =>
363
+ case GetOwnPropertyDescriptorsBuiltin =>
363
364
/* getOwnPropertyDescriptors = (() => {
364
365
* // Fetch or polyfill Reflect.ownKeys
365
366
* var ownKeysFun;
@@ -457,31 +458,24 @@ private[emitter] object CoreJSLib {
457
458
Apply (funGenerator, Nil )
458
459
}
459
460
460
- val mathBuiltins = Block (
461
- List (" imul" , " fround" , " clz32" ).map { builtinName =>
462
- val rhs0 = genIdentBracketSelect(MathRef , builtinName)
463
- val rhs =
464
- if (esVersion >= ESVersion .ES2015 ) rhs0
465
- else rhs0 || genPolyfillFor(builtinName)
466
- extractWithGlobals(globalVarDef(builtinName, CoreVar , rhs))
461
+ val polyfillDefs = for {
462
+ builtin <- PolyfillableBuiltin .All
463
+ if esVersion < builtin.availableInESVersion
464
+ } yield {
465
+ val polyfill = genPolyfillFor(builtin)
466
+ val rhs = builtin match {
467
+ case builtin : GlobalVarBuiltin =>
468
+ // (typeof GlobalVar !== "undefined") ? GlobalVar : polyfill
469
+ val globalVarRef = globalRef(builtin.globalVar)
470
+ If (UnaryOp (JSUnaryOp .typeof, globalVarRef) !== str(" undefined" ),
471
+ globalVarRef, polyfill)
472
+ case builtin : NamespacedBuiltin =>
473
+ // NamespaceGlobalVar.builtinName || polyfill
474
+ genIdentBracketSelect(globalRef(builtin.namespaceGlobalVar), builtin.builtinName) || polyfill
467
475
}
468
- )
469
-
470
- val es5Compat = condTree(esVersion < ESVersion .ES2015 )(Block (
471
- extractWithGlobals(globalVarDef(" is" , CoreVar ,
472
- genIdentBracketSelect(ObjectRef , " is" ) || genPolyfillFor(" is" ))),
473
- extractWithGlobals(globalVarDef(" privateJSFieldSymbol" , CoreVar ,
474
- If (UnaryOp (JSUnaryOp .typeof, SymbolRef ) !== str(" undefined" ),
475
- SymbolRef , genPolyfillFor(" privateJSFieldSymbol" ))))
476
- ))
477
-
478
- val es2017Compat = condTree(esVersion < ESVersion .ES2017 )(Block (
479
- extractWithGlobals(globalVarDef(" getOwnPropertyDescriptors" , CoreVar ,
480
- genIdentBracketSelect(ObjectRef , " getOwnPropertyDescriptors" ) ||
481
- genPolyfillFor(" getOwnPropertyDescriptors" )))
482
- ))
483
-
484
- Block (mathBuiltins, es5Compat, es2017Compat)
476
+ extractWithGlobals(globalVarDef(builtin.builtinName, CoreVar , rhs))
477
+ }
478
+ Block (polyfillDefs)
485
479
}
486
480
487
481
private def declareCachedL0 (): Tree = {
@@ -611,12 +605,8 @@ private[emitter] object CoreJSLib {
611
605
612
606
defineFunction1(" objectClone" ) { instance =>
613
607
// return Object.create(Object.getPrototypeOf(instance), $getOwnPropertyDescriptors(instance));
614
- val callGetOwnPropertyDescriptors = {
615
- if (esVersion >= ESVersion .ES2017 )
616
- Apply (genIdentBracketSelect(ObjectRef , " getOwnPropertyDescriptors" ), instance :: Nil )
617
- else
618
- genCallHelper(" getOwnPropertyDescriptors" , instance)
619
- }
608
+ val callGetOwnPropertyDescriptors = genCallPolyfillableBuiltin(
609
+ GetOwnPropertyDescriptorsBuiltin , instance)
620
610
Return (Apply (genIdentBracketSelect(ObjectRef , " create" ), List (
621
611
Apply (genIdentBracketSelect(ObjectRef , " getPrototypeOf" ), instance :: Nil ),
622
612
callGetOwnPropertyDescriptors)))
@@ -894,7 +884,7 @@ private[emitter] object CoreJSLib {
894
884
(abs & bigInt(~ 0xffffL)) | bigInt(0x8000L)
895
885
})),
896
886
const(absR, Apply (NumberRef , y :: Nil )),
897
- Return (genCallHelper( " fround " , If (x < bigInt(0L ), - absR, absR)))
887
+ Return (genCallPolyfillableBuiltin( FroundBuiltin , If (x < bigInt(0L ), - absR, absR)))
898
888
)
899
889
}
900
890
))
@@ -1188,7 +1178,7 @@ private[emitter] object CoreJSLib {
1188
1178
condTree(strictFloats)(
1189
1179
defineFunction1(" isFloat" ) { v =>
1190
1180
Return ((typeof(v) === str(" number" )) &&
1191
- ((v !== v) || (genCallHelper( " fround " , v) === v)))
1181
+ ((v !== v) || (genCallPolyfillableBuiltin( FroundBuiltin , v) === v)))
1192
1182
}
1193
1183
)
1194
1184
)
@@ -1953,6 +1943,11 @@ private[emitter] object CoreJSLib {
1953
1943
private def genArrowFunction (args : List [ParamDef ], body : Tree ): Function =
1954
1944
jsGen.genArrowFunction(args, None , body)
1955
1945
1946
+ private def genCallPolyfillableBuiltin (builtin : PolyfillableBuiltin ,
1947
+ args : Tree * ): Tree = {
1948
+ extractWithGlobals(sjsGen.genCallPolyfillableBuiltin(builtin, args : _* ))
1949
+ }
1950
+
1956
1951
private def maybeWrapInUBE (behavior : CheckedBehavior , exception : Tree ): Tree = {
1957
1952
if (behavior == CheckedBehavior .Fatal ) {
1958
1953
genScalaClassNew(UndefinedBehaviorErrorClass ,
0 commit comments