Bladeren bron

[js] add js.Lib.parseInt and replace some untyped __js__ in js.Lib with js.Syntax.code

Dan Korostelev 5 jaren geleden
bovenliggende
commit
7fcc3ab503
2 gewijzigde bestanden met toevoegingen van 22 en 9 verwijderingen
  1. 19 7
      std/js/Lib.hx
  2. 3 2
      std/js/_std/Std.hx

+ 19 - 7
std/js/Lib.hx

@@ -31,7 +31,7 @@ class Lib {
 		Inserts a 'debugger' statement that will make a breakpoint if a debugger is available.
 	**/
 	public static inline function debug() {
-		untyped __js__("debugger");
+		js.Syntax.code("debugger");
 	}
 
 	/**
@@ -40,11 +40,11 @@ class Lib {
 	**/
 	@:deprecated("Lib.alert() is deprecated, use Browser.alert() instead")
 	public static function alert(v:Dynamic) {
-		untyped __js__("alert")(js.Boot.__string_rec(v, ""));
+		js.Syntax.code("alert")(@:privateAccess js.Boot.__string_rec(v, ""));
 	}
 
 	public static inline function eval(code:String):Dynamic {
-		return untyped __js__("eval")(code);
+		return js.Syntax.code("eval")(code);
 	}
 
 	/**
@@ -55,7 +55,19 @@ class Lib {
 		is available, such as Node.js or RequireJS.
 	**/
 	extern public static inline function require(module:String):Dynamic {
-		return untyped __js__("require")(module);
+		return js.Syntax.code("require")(module);
+	}
+
+	/**
+		Native JavaScript `parseInt` function.
+
+		Its specification is different from `Std.parseInt`, so one
+		might want to access the native one.
+	**/
+	public static var parseInt(get, never):(string:String, ?radix:Int) -> Float;
+
+	extern static inline function get_parseInt():(string:String, ?radix:Int) -> Float {
+		return js.Syntax.code("parseInt");
 	}
 
 	/**
@@ -68,7 +80,7 @@ class Lib {
 	public static var undefined(get, never):Dynamic;
 
 	static inline function get_undefined():Dynamic {
-		return untyped __js__("undefined");
+		return js.Syntax.code("undefined");
 	}
 
 	/**
@@ -86,7 +98,7 @@ class Lib {
 	public static var nativeThis(get, never):Dynamic;
 
 	extern static inline function get_nativeThis():Dynamic {
-		return untyped __js__("this");
+		return js.Syntax.code("this");
 	}
 
 	/**
@@ -108,7 +120,7 @@ class Lib {
 	public static var global(get, never):Dynamic;
 
 	extern static inline function get_global():Dynamic {
-		return untyped __define_feature__("js.Lib.global", __js__("$global")); // $global is generated by the compiler
+		return untyped __define_feature__("js.Lib.global", js.Syntax.code("$global")); // $global is generated by the compiler
 	}
 
 	/**

+ 3 - 2
std/js/_std/Std.hx

@@ -53,8 +53,9 @@ import js.Syntax;
 			for(i in 0...x.length) {
 				var c = StringTools.fastCodeAt(x, i);
 				if(c <= 8 || (c >= 14 && c != ' '.code && c != '-'.code)) {
-					var v:Int = Syntax.code('parseInt({0}, ({0}[{1}]=="x" || {0}[{1}]=="X") ? 16 : 10)', x, i + 1);
-					return Math.isNaN(v) ? null : v;
+					var nc = StringTools.fastCodeAt(x, i + 1);
+					var v = js.Lib.parseInt(x, (nc == "x".code || nc == "X".code) ? 16 : 10);
+					return Math.isNaN(v) ? null : cast v;
 				}
 			}
 		}