@@ -28,6 +28,7 @@ import org.scalajs.linker.interface.unstable.RuntimeClassNameMapperImpl
2828import org .scalajs .linker .backend .javascript .Trees ._
2929
3030import EmitterNames ._
31+ import PolyfillableBuiltin ._
3132
3233private [emitter] object CoreJSLib {
3334
@@ -167,8 +168,8 @@ private[emitter] object CoreJSLib {
167168 }
168169
169170 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 =>
172173 val x = varRef(" x" )
173174 val y = varRef(" y" )
174175 genArrowFunction(paramList(x, y), Return {
@@ -181,7 +182,7 @@ private[emitter] object CoreJSLib {
181182 })
182183 })
183184
184- case " imul " =>
185+ case ImulBuiltin =>
185186 val a = varRef(" a" )
186187 val b = varRef(" b" )
187188 val ah = varRef(" ah" )
@@ -196,7 +197,7 @@ private[emitter] object CoreJSLib {
196197 Return ((al * bl) + (((ah * bl + al * bh) << 16 ) >>> 0 ) | 0 )
197198 ))
198199
199- case " fround " =>
200+ case FroundBuiltin =>
200201 val v = varRef(" v" )
201202 if (! strictFloats) {
202203 genArrowFunction(paramList(v), Return (+ v))
@@ -300,7 +301,7 @@ private[emitter] object CoreJSLib {
300301 typedArrayPolyfill, noTypedArrayPolyfill)
301302 }
302303
303- case " clz32 " =>
304+ case Clz32Builtin =>
304305 val i = varRef(" i" )
305306 val r = varRef(" r" )
306307 genArrowFunction(paramList(i), Block (
@@ -314,7 +315,7 @@ private[emitter] object CoreJSLib {
314315 Return (r + (i >> 31 ))
315316 ))
316317
317- case " privateJSFieldSymbol " =>
318+ case PrivateSymbolBuiltin =>
318319 /* function privateJSFieldSymbol(description) {
319320 * function rand32() {
320321 * const s = ((Math.random() * 4294967296.0) | 0).toString(16);
@@ -359,7 +360,7 @@ private[emitter] object CoreJSLib {
359360 }
360361 ))
361362
362- case " getOwnPropertyDescriptors " =>
363+ case GetOwnPropertyDescriptorsBuiltin =>
363364 /* getOwnPropertyDescriptors = (() => {
364365 * // Fetch or polyfill Reflect.ownKeys
365366 * var ownKeysFun;
@@ -457,31 +458,24 @@ private[emitter] object CoreJSLib {
457458 Apply (funGenerator, Nil )
458459 }
459460
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
467475 }
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)
485479 }
486480
487481 private def declareCachedL0 (): Tree = {
@@ -611,12 +605,8 @@ private[emitter] object CoreJSLib {
611605
612606 defineFunction1(" objectClone" ) { instance =>
613607 // 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)
620610 Return (Apply (genIdentBracketSelect(ObjectRef , " create" ), List (
621611 Apply (genIdentBracketSelect(ObjectRef , " getPrototypeOf" ), instance :: Nil ),
622612 callGetOwnPropertyDescriptors)))
@@ -894,7 +884,7 @@ private[emitter] object CoreJSLib {
894884 (abs & bigInt(~ 0xffffL)) | bigInt(0x8000L)
895885 })),
896886 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)))
898888 )
899889 }
900890 ))
@@ -1188,7 +1178,7 @@ private[emitter] object CoreJSLib {
11881178 condTree(strictFloats)(
11891179 defineFunction1(" isFloat" ) { v =>
11901180 Return ((typeof(v) === str(" number" )) &&
1191- ((v !== v) || (genCallHelper( " fround " , v) === v)))
1181+ ((v !== v) || (genCallPolyfillableBuiltin( FroundBuiltin , v) === v)))
11921182 }
11931183 )
11941184 )
@@ -1953,6 +1943,11 @@ private[emitter] object CoreJSLib {
19531943 private def genArrowFunction (args : List [ParamDef ], body : Tree ): Function =
19541944 jsGen.genArrowFunction(args, None , body)
19551945
1946+ private def genCallPolyfillableBuiltin (builtin : PolyfillableBuiltin ,
1947+ args : Tree * ): Tree = {
1948+ extractWithGlobals(sjsGen.genCallPolyfillableBuiltin(builtin, args : _* ))
1949+ }
1950+
19561951 private def maybeWrapInUBE (behavior : CheckedBehavior , exception : Tree ): Tree = {
19571952 if (behavior == CheckedBehavior .Fatal ) {
19581953 genScalaClassNew(UndefinedBehaviorErrorClass ,
0 commit comments