Update Files

This commit is contained in:
2025-01-22 16:18:30 +01:00
parent ed4603cf95
commit a36294b518
16718 changed files with 2960346 additions and 0 deletions

View File

@ -0,0 +1,99 @@
/*
* Copyright (C)2005-2019 Haxe Foundation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
import haxe.iterators.ArrayKeyValueIterator;
@:coreApi
extern class Array<T> {
var length(default, null):Int;
function new():Void;
function concat(a:Array<T>):Array<T>;
function join(sep:String):String;
function pop():Null<T>;
function push(x:T):Int;
function reverse():Void;
function shift():Null<T>;
function slice(pos:Int, ?end:Int):Array<T>;
function sort(f:T->T->Int):Void;
function splice(pos:Int, len:Int):Array<T>;
function toString():String;
function unshift(x:T):Void;
inline function insert(pos:Int, x:T):Void {
(cast this).splice(pos, 0, x);
}
inline function remove(x:T):Bool {
return @:privateAccess HxOverrides.remove(this, x);
}
inline function contains(x:T):Bool {
#if (js_es >= 6)
return (cast this).includes(x);
#else
return this.indexOf(x) != -1;
#end
}
#if (js_es >= 5)
@:pure function indexOf(x:T, ?fromIndex:Int):Int;
@:pure function lastIndexOf(x:T, ?fromIndex:Int):Int;
#else
inline function indexOf(x:T, ?fromIndex:Int):Int {
return @:privateAccess HxOverrides.indexOf(this, x, (fromIndex != null) ? fromIndex : 0);
}
inline function lastIndexOf(x:T, ?fromIndex:Int):Int {
return @:privateAccess HxOverrides.lastIndexOf(this, x, (fromIndex != null) ? fromIndex : length - 1);
}
#end
@:pure
inline function copy():Array<T> {
return (cast this).slice();
}
@:runtime inline function map<S>(f:T->S):Array<S> {
var result:Array<S> = js.Syntax.construct(Array, length);
for(i in 0...length) {
result[i] = f(this[i]);
}
return result;
}
@:runtime inline function filter(f:T->Bool):Array<T> {
return [for (v in this) if (f(v)) v];
}
@:runtime inline function iterator():haxe.iterators.ArrayIterator<T> {
return new haxe.iterators.ArrayIterator(this);
}
@:runtime inline function keyValueIterator():ArrayKeyValueIterator<T> {
return new ArrayKeyValueIterator(this);
}
inline function resize(len:Int):Void {
this.length = len;
}
}

View File

@ -0,0 +1,57 @@
/*
* Copyright (C)2005-2019 Haxe Foundation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
@:coreApi extern class Date {
@:pure function new(year:Int, month:Int, day:Int, hour:Int, min:Int, sec:Int):Void;
@:pure function getTime():Float;
@:pure function getHours():Int;
@:pure function getMinutes():Int;
@:pure function getSeconds():Int;
@:pure function getFullYear():Int;
@:pure function getMonth():Int;
@:pure function getDate():Int;
@:pure function getDay():Int;
@:pure function getUTCHours():Int;
@:pure function getUTCMinutes():Int;
@:pure function getUTCSeconds():Int;
@:pure function getUTCFullYear():Int;
@:pure function getUTCMonth():Int;
@:pure function getUTCDate():Int;
@:pure function getUTCDay():Int;
@:pure function getTimezoneOffset():Int;
@:pure inline function toString():String {
return @:privateAccess HxOverrides.dateStr(this);
}
@:pure static inline function now():Date {
return js.Syntax.construct(Date);
}
@:pure static inline function fromTime(t:Float):Date {
return js.Syntax.construct(Date, t);
}
@:pure static inline function fromString(s:String):Date {
return @:privateAccess HxOverrides.strDate(s);
}
}

View File

@ -0,0 +1,125 @@
/*
* Copyright (C)2005-2019 Haxe Foundation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
@:coreApi class EReg {
var r:HaxeRegExp;
public inline function new(r:String, opt:String):Void {
this.r = new HaxeRegExp(r, opt.split("u").join("")); // 'u' (utf8) depends on page encoding
}
public function match(s:String):Bool {
if (r.global)
r.lastIndex = 0;
r.m = r.exec(s);
r.s = s;
return (r.m != null);
}
public function matched(n:Int):String {
return if (r.m != null && n >= 0 && n < r.m.length) r.m[n] else throw "EReg::matched";
}
public function matchedLeft():String {
if (r.m == null)
throw "No string matched";
return r.s.substr(0, r.m.index);
}
public function matchedRight():String {
if (r.m == null)
throw "No string matched";
var sz = r.m.index + r.m[0].length;
return r.s.substr(sz, r.s.length - sz);
}
public function matchedPos():{pos:Int, len:Int} {
if (r.m == null)
throw "No string matched";
return {pos: r.m.index, len: r.m[0].length};
}
public function matchSub(s:String, pos:Int, len:Int = -1):Bool {
return if (r.global) {
r.lastIndex = pos;
r.m = r.exec(len < 0 ? s : s.substr(0, pos + len));
var b = r.m != null;
if (b) {
r.s = s;
}
b;
} else {
// TODO: check some ^/$ related corner cases
var b = match(len < 0 ? s.substr(pos) : s.substr(pos, len));
if (b) {
r.s = s;
r.m.index += pos;
}
b;
}
}
public function split(s:String):Array<String> {
// we can't use directly s.split because it's ignoring the 'g' flag
var d = "#__delim__#";
return replace(s, d).split(d);
}
public inline function replace(s:String, by:String):String {
return (cast s).replace(r, by);
}
public function map(s:String, f:EReg->String):String {
var offset = 0;
var buf = new StringBuf();
do {
if (offset >= s.length)
break;
else if (!matchSub(s, offset)) {
buf.add(s.substr(offset));
break;
}
var p = matchedPos();
buf.add(s.substr(offset, p.pos - offset));
buf.add(f(this));
if (p.len == 0) {
buf.add(s.substr(p.pos, 1));
offset = p.pos + 1;
} else
offset = p.pos + p.len;
} while (r.global);
if (!r.global && offset > 0 && offset < s.length)
buf.add(s.substr(offset));
return buf.toString();
}
public static inline function escape(s:String):String {
return (cast s).replace(escapeRe, "\\$&");
}
static var escapeRe = new js.lib.RegExp("[.*+?^${}()|[\\]\\\\]", "g");
}
@:native("RegExp")
private extern class HaxeRegExp extends js.lib.RegExp {
var m:js.lib.RegExp.RegExpMatch;
var s:String;
}

View File

@ -0,0 +1,164 @@
/*
* Copyright (C)2005-2019 Haxe Foundation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
@:noDoc
class HxOverrides {
static function dateStr(date:Date):String {
var m = date.getMonth() + 1;
var d = date.getDate();
var h = date.getHours();
var mi = date.getMinutes();
var s = date.getSeconds();
return date.getFullYear() + "-" + (if (m < 10) "0" + m else "" + m) + "-" + (if (d < 10) "0" + d else "" + d) + " "
+ (if (h < 10) "0" + h else "" + h) + ":" + (if (mi < 10) "0" + mi else "" + mi) + ":" + (if (s < 10) "0" + s else "" + s);
}
static function strDate(s:String):Date {
switch (s.length) {
case 8: // hh:mm:ss
var k = s.split(":");
var d = js.Syntax.construct(Date);
(cast d)[cast "setTime"](0);
(cast d)[cast "setUTCHours"](k[0]);
(cast d)[cast "setUTCMinutes"](k[1]);
(cast d)[cast "setUTCSeconds"](k[2]);
return d;
case 10: // YYYY-MM-DD
var k = s.split("-");
return new Date(cast k[0], (cast k[1]) - 1, cast k[2], 0, 0, 0);
case 19: // YYYY-MM-DD hh:mm:ss
var k = s.split(" ");
var y = k[0].split("-");
var t = k[1].split(":");
return new Date(cast y[0], (cast y[1]) - 1, cast y[2], cast t[0], cast t[1], cast t[2]);
default:
throw "Invalid date format : " + s;
}
}
@:pure
static function cca(s:String, index:Int):Null<Int> {
var x = (cast s).charCodeAt(index);
if (x != x) // fast isNaN
return js.Lib.undefined; // isNaN will still return true
return x;
}
@:pure
static function substr(s:String, pos:Int, ?len:Int):String {
if (len == null) {
len = s.length;
} else if (len < 0) {
if (pos == 0)
len = s.length + len;
else
return "";
}
#if (js_es < 5)
if (pos < 0) {
pos = s.length + pos;
if (pos < 0)
pos = 0;
}
#end
return (cast s).substr(pos, len);
}
@:pure
static function indexOf<T>(a:Array<T>, obj:T, i:Int) {
var len = a.length;
if (i < 0) {
i += len;
if (i < 0)
i = 0;
}
while (i < len) {
if (js.Syntax.strictEq(a[i], obj))
return i;
i++;
}
return -1;
}
@:pure
static function lastIndexOf<T>(a:Array<T>, obj:T, i:Int) {
var len = a.length;
if (i >= len)
i = len - 1;
else if (i < 0)
i += len;
while (i >= 0) {
if (js.Syntax.strictEq(a[i], obj))
return i;
i--;
}
return -1;
}
static function remove<T>(a:Array<T>, obj:T) {
var i = a.indexOf(obj);
if (i == -1)
return false;
a.splice(i, 1);
return true;
}
@:pure
static function iter<T>(a:Array<T>):Iterator<T>
untyped {
return {
cur: 0,
arr: a,
hasNext: function() {
return __this__.cur < __this__.arr.length;
},
next: function() {
return __this__.arr[__this__.cur++];
}
};
}
@:ifFeature("anon_read.keyValueIterator", "dynamic_read.keyValueIterator", "closure_read.keyValueIterator")
static function keyValueIter<T>( a : Array<T> ) {
return new haxe.iterators.ArrayKeyValueIterator(a);
}
@:pure
static function now(): Float return js.lib.Date.now();
static function __init__()
untyped {
#if (js_es < 5)
__feature__('HxOverrides.indexOf',
if (Array.prototype.indexOf) js.Syntax.code("HxOverrides").indexOf = function(a, o, i) return Array.prototype.indexOf.call(a, o, i));
__feature__('HxOverrides.lastIndexOf',
if (Array.prototype.lastIndexOf) js.Syntax.code("HxOverrides").lastIndexOf = function(a, o, i) return Array.prototype.lastIndexOf.call(a, o, i));
#end
__feature__('HxOverrides.now',
if (js.Syntax.typeof(performance) != 'undefined' && js.Syntax.typeof(performance.now) == 'function') {
HxOverrides.now = performance.now.bind(performance);
}
);
}
}

View File

@ -0,0 +1,87 @@
/*
* Copyright (C)2005-2019 Haxe Foundation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
import js.Syntax.code;
// Can't enable @:coreApi because some fields are now inline getters
// @:coreApi
@:keepInit
extern class Math {
static var PI(default, null):Float;
static var NEGATIVE_INFINITY(get, null):Float;
@:pure private static inline function get_NEGATIVE_INFINITY():Float {
return -code("Infinity");
}
static var POSITIVE_INFINITY(get, null):Float;
@:pure private static inline function get_POSITIVE_INFINITY():Float {
return code("Infinity");
}
static var NaN(get, null):Float;
@:pure private static inline function get_NaN():Float {
return code("NaN");
}
@:pure static function abs(v:Float):Float;
@:pure static function acos(v:Float):Float;
@:pure static function asin(v:Float):Float;
@:pure static function atan(v:Float):Float;
@:pure static function atan2(y:Float, x:Float):Float;
@:pure static function ceil(v:Float):Int;
@:pure static function cos(v:Float):Float;
@:pure static function exp(v:Float):Float;
@:pure static function floor(v:Float):Int;
@:pure static function log(v:Float):Float;
@:pure static function max(a:Float, b:Float):Float;
@:pure static function min(a:Float, b:Float):Float;
@:pure static function pow(v:Float, exp:Float):Float;
static function random():Float;
@:pure static function round(v:Float):Int;
@:pure static function sin(v:Float):Float;
@:pure static function sqrt(v:Float):Float;
@:pure static function tan(v:Float):Float;
@:pure static inline function ffloor(v:Float):Float {
return floor(v);
}
@:pure static inline function fceil(v:Float):Float {
return ceil(v);
}
@:pure static inline function fround(v:Float):Float {
return round(v);
}
@:pure static inline function isFinite(f:Float):Bool {
return code("isFinite")(f);
}
@:pure static inline function isNaN(f:Float):Bool {
return code("isNaN")(f);
}
static function __init__():Void {
untyped __feature__("Type.resolveClass", $hxClasses["Math"] = Math);
}
}

View File

@ -0,0 +1,126 @@
/*
* Copyright (C)2005-2019 Haxe Foundation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
@:coreApi class Reflect {
@:pure
public inline static function hasField(o:Dynamic, field:String):Bool {
return js.lib.Object.prototype.hasOwnProperty.call(o, field);
}
@:pure
public static function field(o:Dynamic, field:String):Dynamic {
try
return o[cast field]
catch (e:Dynamic)
return null;
}
public inline static function setField(o:Dynamic, field:String, value:Dynamic):Void {
o[cast field] = value;
}
public static function getProperty(o:Dynamic, field:String):Dynamic
untyped {
var tmp;
return if (o == null) __define_feature__("Reflect.getProperty",
null) else if (o.__properties__ && (tmp = o.__properties__["get_" + field])) o[tmp]() else o[field];
}
public static function setProperty(o:Dynamic, field:String, value:Dynamic):Void
untyped {
var tmp;
if (o.__properties__ && (tmp = o.__properties__["set_" + field]))
o[tmp](value)
else
o[field] = __define_feature__("Reflect.setProperty", value);
}
public inline static function callMethod(o:Dynamic, func:haxe.Constraints.Function, args:Array<Dynamic>):Dynamic {
return (cast func : js.lib.Function).apply(o, args);
}
public static function fields(o:Dynamic):Array<String> {
var a = [];
if (o != null)
untyped {
var hasOwnProperty = js.lib.Object.prototype.hasOwnProperty;
js.Syntax.code("for( var f in o ) {");
if (f != "__id__" && f != "hx__closures__" && hasOwnProperty.call(o, f))
a.push(f);
js.Syntax.code("}");
}
return a;
}
@:access(js.Boot)
public static function isFunction(f:Dynamic):Bool {
return js.Syntax.typeof(f) == "function" && !(js.Boot.isClass(f) || js.Boot.isEnum(f));
}
public static function compare<T>(a:T, b:T):Int {
return (a == b) ? 0 : (((cast a) > (cast b)) ? 1 : -1);
}
public static function compareMethods(f1:Dynamic, f2:Dynamic):Bool {
if (f1 == f2)
return true;
if (!isFunction(f1) || !isFunction(f2))
return false;
return f1.scope == f2.scope && f1.method == f2.method && f1.method != null;
}
@:access(js.Boot)
public static function isObject(v:Dynamic):Bool {
if (v == null)
return false;
var t = js.Syntax.typeof(v);
return (t == "string" || (t == "object" && v.__enum__ == null))
|| (t == "function" && (js.Boot.isClass(v) || js.Boot.isEnum(v)) != null);
}
public static function isEnumValue(v:Dynamic):Bool {
return v != null && v.__enum__ != null;
}
public static function deleteField(o:Dynamic, field:String):Bool {
if (!hasField(o, field))
return false;
js.Syntax.delete(o, field);
return true;
}
public static function copy<T>(o:Null<T>):Null<T> {
if (o == null)
return null;
var o2:Dynamic = {};
for (f in Reflect.fields(o))
Reflect.setField(o2, f, Reflect.field(o, f));
return o2;
}
@:overload(function(f:Array<Dynamic>->Void):Dynamic {})
public static function makeVarArgs(f:Array<Dynamic>->Dynamic):Dynamic {
return function() {
var a = untyped Array.prototype.slice.call(js.Syntax.code("arguments"));
return f(a);
};
}
}

View File

@ -0,0 +1,113 @@
/*
* Copyright (C)2005-2019 Haxe Foundation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
import js.Boot;
import js.Syntax;
@:keepInit
@:coreApi class Std {
@:deprecated('Std.is is deprecated. Use Std.isOfType instead.')
public static inline function is(v:Dynamic, t:Dynamic):Bool {
return isOfType(v, t);
}
public static inline function isOfType(v:Dynamic, t:Dynamic):Bool {
return @:privateAccess js.Boot.__instanceof(v, t);
}
public static inline function downcast<T:{}, S:T>(value:T, c:Class<S>):S@:privateAccess {
return if (js.Boot.__downcastCheck(value, c)) cast value else null;
}
@:deprecated('Std.instance() is deprecated. Use Std.downcast() instead.')
public static inline function instance<T:{}, S:T>(value:T, c:Class<S>):S {
return downcast(value, c);
}
@:pure
public static function string(s:Dynamic):String {
return @:privateAccess js.Boot.__string_rec(s, "");
}
public static inline function int(x:Float):Int {
return (cast x) | 0;
}
@:pure
public static function parseInt(x:String):Null<Int> {
if(x != null) {
for(i in 0...x.length) {
var c = StringTools.fastCodeAt(x, i);
if(c <= 8 || (c >= 14 && c != ' '.code && c != '-'.code)) {
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;
}
}
}
return null;
}
public static inline function parseFloat(x:String):Float {
return js.Syntax.code("parseFloat({0})", x);
}
public static function random(x:Int):Int {
return x <= 0 ? 0 : Math.floor(Math.random() * x);
}
static function __init__():Void
untyped {
__feature__("js.Boot.getClass", String.prototype.__class__ = __feature__("Type.resolveClass", $hxClasses["String"] = String, String));
__feature__("js.Boot.isClass", String.__name__ = __feature__("Type.getClassName", "String", true));
__feature__("Type.resolveClass", $hxClasses["Array"] = Array);
__feature__("js.Boot.isClass", Array.__name__ = __feature__("Type.getClassName", "Array", true));
__feature__("Date.*", {
__feature__("js.Boot.getClass",
js.Syntax.code('Date').prototype.__class__ = __feature__("Type.resolveClass", $hxClasses["Date"] = js.Syntax.code('Date'), js.Syntax.code('Date')));
__feature__("js.Boot.isClass", js.Syntax.code('Date').__name__ = "Date");
});
__feature__("Int.*", js.Syntax.code('var Int = { };'));
__feature__("Dynamic.*", js.Syntax.code('var Dynamic = { };'));
__feature__("Float.*", js.Syntax.code('var Float = Number'));
__feature__("Bool.*", js.Syntax.code('var Bool = Boolean'));
__feature__("Class.*", js.Syntax.code('var Class = { };'));
__feature__("Enum.*", js.Syntax.code('var Enum = { };'));
#if (js_es < 5)
__feature__("Array.map", if (Array.prototype.map == null) Array.prototype.map = function(f) {
var a = [];
for (i in 0...__this__.length)
a[i] = f(__this__[i]);
return a;
});
__feature__("Array.filter", if (Array.prototype.filter == null) Array.prototype.filter = function(f) {
var a = [];
for (i in 0...__this__.length) {
var e = __this__[i];
if (f(e))
a.push(e);
}
return a;
});
#end
}
}

View File

@ -0,0 +1,51 @@
/*
* Copyright (C)2005-2019 Haxe Foundation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
@:coreApi extern class String {
var length(default, null):Int;
@:pure function new(string:String):Void;
@:pure function toUpperCase():String;
@:pure function toLowerCase():String;
@:pure function charAt(index:Int):String;
@:pure function indexOf(str:String, ?startIndex:Int):Int;
@:pure function lastIndexOf(str:String, ?startIndex:Int):Int;
@:pure function split(delimiter:String):Array<String>;
@:pure function toString():String;
@:pure function substring(startIndex:Int, ?endIndex:Int):String;
@:pure inline function charCodeAt(index:Int):Null<Int> {
return @:privateAccess HxOverrides.cca(this, index);
}
@:pure inline function substr(pos:Int, ?len:Int):String {
return @:privateAccess HxOverrides.substr(this, pos, len);
}
@:pure static inline function fromCharCode(code:Int):String {
return untyped __define_feature__('String.fromCharCode', js.Syntax.code("String.fromCodePoint({0})", code));
}
static function __init__():Void {
untyped __feature__('String.fromCharCode',
js.Syntax.code("if( String.fromCodePoint == null ) String.fromCodePoint = function(c) { return c < 0x10000 ? String.fromCharCode(c) : String.fromCharCode((c>>10)+0xD7C0)+String.fromCharCode((c&0x3FF)+0xDC00); }"));
}
}

View File

@ -0,0 +1,338 @@
/*
* Copyright (C)2005-2019 Haxe Foundation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
enum ValueType {
TNull;
TInt;
TFloat;
TBool;
TObject;
TFunction;
TClass(c:Class<Dynamic>);
TEnum(e:Enum<Dynamic>);
TUnknown;
}
@:coreApi class Type {
public static inline function getClass<T>(o:T):Class<T> {
return @:privateAccess js.Boot.getClass(o);
}
public static function getEnum(o:EnumValue):Enum<Dynamic>
untyped {
if (o == null)
return null;
#if js_enums_as_arrays
return o.__enum__;
#else
return $hxEnums[o.__enum__];
#end
}
public static inline function getSuperClass(c:Class<Dynamic>):Class<Dynamic> {
return untyped __define_feature__("Type.getSuperClass", c.__super__);
}
public static inline function getClassName(c:Class<Dynamic>):String {
return untyped __define_feature__("Type.getClassName", c.__name__);
}
public static inline function getEnumName(e:Enum<Dynamic>):String {
return untyped __define_feature__("Type.getEnumName", e.__ename__);
}
#if js_enums_as_arrays
public static function resolveClass(name:String):Class<Dynamic>
untyped {
var cl:Class<Dynamic> = $hxClasses[name];
// ensure that this is a class
if (cl == null || !js.Boot.isClass(cl))
return null;
return cl;
}
public static function resolveEnum(name:String):Enum<Dynamic>
untyped {
var e:Dynamic = $hxClasses[name];
// ensure that this is an enum
if (e == null || !js.Boot.isEnum(e))
return null;
return e;
}
#else
public static inline function resolveClass(name:String):Class<Dynamic> {
return untyped __define_feature__("Type.resolveClass", $hxClasses[name]);
}
public static inline function resolveEnum(name:String):Enum<Dynamic> {
return untyped __define_feature__("Type.resolveEnum", $hxEnums[name]);
}
#end
#if (js_es < 5)
public static function createInstance<T>(cl:Class<T>, args:Array<Dynamic>):T {
switch (args.length) {
case 0:
return js.Syntax.construct(cl);
case 1:
return js.Syntax.construct(cl, args[0]);
case 2:
return js.Syntax.construct(cl, args[0], args[1]);
case 3:
return js.Syntax.construct(cl, args[0], args[1], args[2]);
case 4:
return js.Syntax.construct(cl, args[0], args[1], args[2], args[3]);
case 5:
return js.Syntax.construct(cl, args[0], args[1], args[2], args[3], args[4]);
case 6:
return js.Syntax.construct(cl, args[0], args[1], args[2], args[3], args[4], args[5]);
case 7:
return js.Syntax.construct(cl, args[0], args[1], args[2], args[3], args[4], args[5], args[6]);
case 8:
return js.Syntax.construct(cl, args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7]);
case 9:
return js.Syntax.construct(cl, args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7], args[8]);
case 10:
return js.Syntax.construct(cl, args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7], args[8], args[9]);
case 11:
return js.Syntax.construct(cl, args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7], args[8], args[9], args[10]);
case 12:
return js.Syntax.construct(cl, args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7], args[8], args[9], args[10], args[11]);
case 13:
return js.Syntax.construct(cl, args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7], args[8], args[9], args[10], args[11],
args[12]);
case 14:
return js.Syntax.construct(cl, args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7], args[8], args[9], args[10], args[11],
args[12], args[13]);
default:
throw "Too many arguments";
}
}
public static function createEmptyInstance<T>(cl:Class<T>):T
untyped {
js.Syntax.code("function empty() {}; empty.prototype = cl.prototype");
return js.Syntax.code("new empty()");
}
#else
public static function createInstance<T>(cl:Class<T>, args:Array<Dynamic>):T {
var ctor = ((cast js.lib.Function).prototype.bind : js.lib.Function).apply(cl, [null].concat(args));
return js.Syntax.code("new ({0})", ctor); // cannot use `js.Syntax.construct` because we need parens if `ctor` is fused in
}
public static inline function createEmptyInstance<T>(cl:Class<T>):T {
return js.lib.Object.create((cast cl).prototype);
}
#end
public static function createEnum<T>(e:Enum<T>, constr:String, ?params:Array<Dynamic>):T {
var f:Dynamic = Reflect.field(e, constr);
if (f == null)
throw "No such constructor " + constr;
if (Reflect.isFunction(f)) {
if (params == null)
throw "Constructor " + constr + " need parameters";
return Reflect.callMethod(e, f, params);
}
if (params != null && params.length != 0)
throw "Constructor " + constr + " does not need parameters";
return f;
}
public static function createEnumIndex<T>(e:Enum<T>, index:Int, ?params:Array<Dynamic>):T {
#if js_enums_as_arrays
var c:String = (untyped e.__constructs__)[index];
#else
var c:String = switch (untyped e.__constructs__)[index] {
case null: null;
case ctor: ctor._hx_name;
}
#end
if (c == null)
throw index + " is not a valid enum constructor index";
return createEnum(e, c, params);
}
#if (js_es >= 6)
public static function getInstanceFields(c:Class<Dynamic>):Array<String> {
var result = [];
while (c != null) {
for (name in js.lib.Object.getOwnPropertyNames((cast c).prototype)) {
switch name {
case "constructor" | "__class__" | "__properties__":
// skip special names
case _:
if (result.indexOf(name) == -1)
result.push(name);
}
}
c = getSuperClass(c);
}
return result;
}
public static function getClassFields(c:Class<Dynamic>):Array<String> {
var a = js.lib.Object.getOwnPropertyNames(cast c);
a.remove("__id__");
a.remove("hx__closures__");
a.remove("__name__");
a.remove("__interfaces__");
a.remove("__isInterface__");
a.remove("__properties__");
a.remove("__instanceFields__");
a.remove("__super__");
a.remove("__meta__");
a.remove("prototype");
a.remove("name");
a.remove("length");
return a;
}
#else
public static function getInstanceFields(c:Class<Dynamic>):Array<String> {
var a = [];
js.Syntax.code("for(var i in c.prototype) a.push(i)");
a.remove("__class__");
a.remove("__properties__");
return a;
}
public static function getClassFields(c:Class<Dynamic>):Array<String> {
var a = Reflect.fields(c);
a.remove("__name__");
a.remove("__interfaces__");
a.remove("__properties__");
a.remove("__super__");
a.remove("__meta__");
a.remove("prototype");
return a;
}
#end
public static inline function getEnumConstructs(e:Enum<Dynamic>):Array<String> {
#if js_enums_as_arrays
return ((cast e).__constructs__ : Array<String>).copy();
#else
return ((cast e).__constructs__ : Array<{_hx_name:String}>).map(c -> c._hx_name);
#end
}
@:access(js.Boot)
public static function typeof(v:Dynamic):ValueType {
switch (js.Syntax.typeof(v)) {
case "boolean":
return TBool;
case "string":
return TClass(String);
case "number":
// this should handle all cases : NaN, +/-Inf and Floats outside range
if (Math.ceil(v) == v % 2147483648.0)
return TInt;
return TFloat;
case "object":
if (v == null)
return TNull;
var e = v.__enum__;
if (e != null) {
#if js_enums_as_arrays
return TEnum(e);
#else
return TEnum(untyped $hxEnums[e]);
#end
}
var c = js.Boot.getClass(v);
if (c != null)
return TClass(c);
return TObject;
case "function":
if (js.Boot.isClass(v) || js.Boot.isEnum(v))
return TObject;
return TFunction;
case "undefined":
return TNull;
default:
return TUnknown;
}
}
public static function enumEq<T:EnumValue>(a:T, b:T):Bool
untyped {
if (a == b)
return true;
try {
var e = a.__enum__;
if (e == null || e != b.__enum__)
return false;
#if js_enums_as_arrays
if (a[0] != b[0])
return false;
for (i in 2...a.length)
if (!enumEq(a[i], b[i]))
return false;
#else
if (a._hx_index != b._hx_index)
return false;
var enm = $hxEnums[e];
var params:Array<String> = enm.__constructs__[a._hx_index].__params__;
for (f in params) {
if (!enumEq(a[f], b[f])) {
return false;
}
}
#end
} catch (e:Dynamic) {
return false;
}
return true;
}
public inline static function enumConstructor(e:EnumValue):String {
#if js_enums_as_arrays
return untyped e[0];
#else
return untyped $hxEnums[e.__enum__].__constructs__[e._hx_index]._hx_name;
#end
}
#if js_enums_as_arrays
public inline static function enumParameters(e:EnumValue):Array<Dynamic> {
return untyped e.slice(2);
}
#else
public static function enumParameters(e:EnumValue):Array<Dynamic>
untyped {
var enm:Enum<Dynamic> = $hxEnums[e.__enum__];
var params:Array<String> = enm.__constructs__[e._hx_index].__params__;
return params != null ? [for (p in params) e[p]] : [];
}
#end
public inline static function enumIndex(e:EnumValue):Int {
#if !js_enums_as_arrays
return untyped e._hx_index;
#else
return untyped e[1];
#end
}
public inline static function allEnums<T>(e:Enum<T>):Array<T> {
return untyped __define_feature__("Type.allEnums", e.__empty_constructs__.slice());
}
}

View File

@ -0,0 +1,171 @@
package haxe;
import js.lib.Error;
@:coreApi
class Exception extends NativeException {
public var message(get,never):String;
public var stack(get,never):CallStack;
public var previous(get,never):Null<Exception>;
public var native(get,never):Any;
@:ifFeature("haxe.Exception.get_stack")
@:noCompletion var __skipStack:Int;
@:noCompletion var __exceptionStack(get,set):Null<CallStack>;
@:noCompletion var __nativeException:Any;
@:noCompletion var __previousException:Null<Exception>;
static function caught(value:Any):Exception {
if(Std.isOfType(value, Exception)) {
return value;
} else if(Std.isOfType(value, Error)) {
return new Exception((cast value:Error).message, null, value);
} else {
return new ValueException(value, null, value);
}
}
static function thrown(value:Any):Any {
if(Std.isOfType(value, Exception)) {
return (value:Exception).native;
} else if(Std.isOfType(value, Error)) {
return value;
} else {
var e = new ValueException(value);
untyped __feature__("haxe.Exception.get_stack", e.__shiftStack());
return e;
}
}
public function new(message:String, ?previous:Exception, ?native:Any) {
super(message);
(cast this).message = message;
__previousException = previous;
__nativeException = native != null ? native : this;
untyped __feature__('haxe.Exception.stack', {
__skipStack = 0;
var old = js.Syntax.code('Error.prepareStackTrace');
js.Syntax.code('Error.prepareStackTrace = function(e) { return e.stack; }');
if(Std.isOfType(native, Error)) {
(cast this).stack = native.stack;
} else {
var e:Error = null;
if ((cast Error).captureStackTrace) {
(cast Error).captureStackTrace(this, Exception);
e = cast this;
} else {
e = new Error();
//Internet Explorer provides call stack only if error was thrown
if(js.Syntax.typeof(e.stack) == "undefined") {
js.Syntax.code('try { throw {0}; } catch(_) {}', e);
__skipStack++;
}
}
(cast this).stack = e.stack;
}
js.Syntax.code('Error.prepareStackTrace = {0}', old);
});
}
function unwrap():Any {
return __nativeException;
}
public function toString():String {
return message;
}
public function details():String {
return inline CallStack.exceptionToString(this);
}
@:noCompletion
@:ifFeature("haxe.Exception.get_stack")
inline function __shiftStack():Void {
__skipStack++;
}
function get_message():String {
return (cast this:Error).message;
}
function get_previous():Null<Exception> {
return __previousException;
}
final function get_native():Any {
return __nativeException;
}
@:ifFeature('haxe.NativeStackTrace.exceptionStack')
function get_stack():CallStack {
return switch __exceptionStack {
case null:
__exceptionStack = NativeStackTrace.toHaxe(NativeStackTrace.normalize((cast this).stack), __skipStack);
case s: s;
}
}
@:noCompletion
function setProperty(name:String, value:Any):Void {
try {
js.lib.Object.defineProperty(this, name, {value:value});
} catch(e:Exception) {
js.Syntax.code('{0}[{1}] = {2}', this, name, value);
}
}
@:noCompletion
inline function get___exceptionStack():CallStack {
return (cast this).__exceptionStack;
}
@:noCompletion
inline function set___exceptionStack(value:CallStack):CallStack {
setProperty('__exceptionStack', value);
return value;
}
@:noCompletion
inline function get___skipStack():Int {
return (cast this).__skipStack;
}
@:noCompletion
inline function set___skipStack(value:Int):Int {
setProperty('__skipStack', value);
return value;
}
@:noCompletion
inline function get___nativeException():Any {
return (cast this).__nativeException;
}
@:noCompletion
inline function set___nativeException(value:Any):Any {
setProperty('__nativeException', value);
return value;
}
@:noCompletion
inline function get___previousException():Null<Exception> {
return (cast this).__previousException;
}
@:noCompletion
inline function set___previousException(value:Null<Exception>):Null<Exception> {
setProperty('__previousException', value);
return value;
}
}
@:dox(hide)
@:noCompletion
@:native('Error')
private extern class NativeException {
// private var message:String; //redefined in haxe.Exception
// private var stack(default, null):String; //redefined in haxe.Exception
function new(?message:String);
}

View File

@ -0,0 +1,46 @@
/*
* Copyright (C)2005-2019 Haxe Foundation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
package haxe;
@:coreApi
#if !haxeJSON
@:native("JSON")
extern
#end
class Json {
#if haxeJSON
inline
#end
public static function parse(text:String):Dynamic
#if !haxeJSON; #else {
return haxe.format.JsonParser.parse(text);
} #end
#if haxeJSON
inline
#end
public static function stringify(value:Dynamic, ?replacer:(key:Dynamic, value:Dynamic) -> Dynamic, ?space:String):String
#if !haxeJSON; #else {
return haxe.format.JsonPrinter.print(value, replacer, space);
} #end
}

View File

@ -0,0 +1,150 @@
package haxe;
import js.Syntax;
import js.lib.Error;
import haxe.CallStack.StackItem;
// https://v8.dev/docs/stack-trace-api
@:native("Error")
private extern class V8Error {
static var prepareStackTrace:(error:Error, structuredStackTrace:Array<V8CallSite>)->Any;
}
typedef V8CallSite = {
function getFunctionName():String;
function getFileName():String;
function getLineNumber():Int;
function getColumnNumber():Int;
}
/**
Do not use manually.
**/
@:dox(hide)
@:noCompletion
@:allow(haxe.Exception)
class NativeStackTrace {
static var lastError:Error;
// support for source-map-support module
@:noCompletion
public static var wrapCallSite:V8CallSite->V8CallSite;
@:ifFeature('haxe.NativeStackTrace.exceptionStack')
static public inline function saveStack(e:Error):Void {
lastError = e;
}
static public function callStack():Any {
var e:Null<Error> = new Error('');
var stack = tryHaxeStack(e);
//Internet Explorer provides call stack only if error was thrown
if(Syntax.typeof(stack) == "undefined") {
try throw e catch(e:Exception) {}
stack = e.stack;
}
return normalize(stack, 2);
}
static public function exceptionStack():Any {
return normalize(tryHaxeStack(lastError));
}
static public function toHaxe(s:Null<Any>, skip:Int = 0):Array<StackItem> {
if (s == null) {
return [];
} else if (Syntax.typeof(s) == "string") {
// Return the raw lines in browsers that don't support prepareStackTrace
var stack:Array<String> = (s:String).split("\n");
if (stack[0] == "Error")
stack.shift();
var m = [];
for (i in 0...stack.length) {
if(skip > i) continue;
var line = stack[i];
var matched:Null<Array<String>> = Syntax.code('{0}.match(/^ at ([A-Za-z0-9_. ]+) \\(([^)]+):([0-9]+):([0-9]+)\\)$/)', line);
if (matched != null) {
var path = matched[1].split(".");
if(path[0] == "$hxClasses") {
path.shift();
}
var meth = path.pop();
var file = matched[2];
var line = Std.parseInt(matched[3]);
var column = Std.parseInt(matched[4]);
m.push(FilePos(meth == "Anonymous function" ? LocalFunction() : meth == "Global code" ? null : Method(path.join("."), meth), file, line,
column));
} else {
m.push(Module(StringTools.trim(line))); // A little weird, but better than nothing
}
}
return m;
} else if(skip > 0 && Syntax.code('Array.isArray({0})', s)) {
return (s:Array<StackItem>).slice(skip);
} else {
return cast s;
}
}
static function tryHaxeStack(e:Null<Error>):Any {
if (e == null) {
return [];
}
// https://v8.dev/docs/stack-trace-api
var oldValue = V8Error.prepareStackTrace;
V8Error.prepareStackTrace = prepareHxStackTrace;
var stack = e.stack;
V8Error.prepareStackTrace = oldValue;
return stack;
}
static function prepareHxStackTrace(e:Error, callsites:Array<V8CallSite>):Any {
var stack = [];
for (site in callsites) {
if (wrapCallSite != null)
site = wrapCallSite(site);
var method = null;
var fullName = site.getFunctionName();
if (fullName != null) {
var idx = fullName.lastIndexOf(".");
if (idx >= 0) {
var className = fullName.substring(0, idx);
var methodName = fullName.substring(idx + 1);
method = Method(className, methodName);
} else {
method = Method(null, fullName);
}
}
var fileName = site.getFileName();
var fileAddr = fileName == null ? -1 : fileName.indexOf("file:");
if (wrapCallSite != null && fileAddr > 0)
fileName = fileName.substring(fileAddr + 6);
stack.push(FilePos(method, fileName, site.getLineNumber(), site.getColumnNumber()));
}
return stack;
}
static function normalize(stack:Any, skipItems:Int = 0):Any {
if(Syntax.code('Array.isArray({0})', stack) && skipItems > 0) {
return (stack:Array<StackItem>).slice(skipItems);
} else if(Syntax.typeof(stack) == "string") {
switch (stack:String).substring(0, 6) {
case 'Error:' | 'Error\n': skipItems += 1;
case _:
}
return skipLines(stack, skipItems);
} else {
//nothing we can do
return stack;
}
}
static function skipLines(stack:String, skip:Int, pos:Int = 0):String {
return if(skip > 0) {
pos = stack.indexOf('\n', pos);
return pos < 0 ? '' : skipLines(stack, --skip, pos + 1);
} else {
return stack.substring(pos);
}
}
}

View File

@ -0,0 +1,100 @@
/*
* Copyright (C)2005-2019 Haxe Foundation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
package haxe.ds;
@:coreApi class IntMap<T> implements haxe.Constraints.IMap<Int, T> {
private var h:Dynamic;
public inline function new():Void {
h = {};
}
public inline function set(key:Int, value:T):Void {
h[key] = value;
}
public inline function get(key:Int):Null<T> {
return h[key];
}
public inline function exists(key:Int):Bool {
return (cast h).hasOwnProperty(key);
}
public function remove(key:Int):Bool {
if (!(cast h).hasOwnProperty(key))
return false;
js.Syntax.delete(h, key);
return true;
}
public function keys():Iterator<Int> {
var a = [];
js.Syntax.code("for( var key in {0} ) if({0}.hasOwnProperty(key)) {1}.push(+key)", h, a);
return a.iterator();
}
public function iterator():Iterator<T> {
return untyped {
ref: h,
it: keys(),
hasNext: function() {
return __this__.it.hasNext();
},
next: function() {
var i = __this__.it.next();
return __this__.ref[i];
}
};
}
@:runtime public inline function keyValueIterator():KeyValueIterator<Int, T> {
return new haxe.iterators.MapKeyValueIterator(this);
}
public function copy():IntMap<T> {
var copied = new IntMap();
for (key in keys())
copied.set(key, get(key));
return copied;
}
public function toString():String {
var s = new StringBuf();
s.add("{");
var it = keys();
for (i in it) {
s.add(i);
s.add(" => ");
s.add(Std.string(get(i)));
if (it.hasNext())
s.add(", ");
}
s.add("}");
return s.toString();
}
public inline function clear():Void {
h = {};
}
}

View File

@ -0,0 +1,132 @@
/*
* Copyright (C)2005-2019 Haxe Foundation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of h software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and h permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
package haxe.ds;
import js.Syntax;
import js.Lib;
@:coreApi
class ObjectMap<K:{}, V> implements haxe.Constraints.IMap<K, V> {
static var count:Int;
// initialize count through __init__ magic, because these are generated
// before normal static initializations for which ObjectMap should be ready to use
// see https://github.com/HaxeFoundation/haxe/issues/6792
static inline function __init__():Void
count = 0;
static inline function assignId(obj:{}):Int {
return Syntax.code('({0}.__id__ = {1})', obj, Lib.getNextHaxeUID());
}
static inline function getId(obj:{}):Int {
return untyped obj.__id__;
}
var h:{__keys__:{}};
public function new():Void {
h = {__keys__: {}};
}
public function set(key:K, value:V):Void {
var id = getId(key);
if(id == null) {
id = assignId(key);
}
Syntax.code('{0}[{1}] = {2}', h, id, value);
Syntax.code('{0}[{1}] = {2}', h.__keys__, id, key);
}
public inline function get(key:K):Null<V> {
return untyped h[getId(key)];
}
public inline function exists(key:K):Bool {
return untyped h.__keys__[getId(key)] != null;
}
public function remove(key:K):Bool {
var id = getId(key);
if (untyped h.__keys__[id] == null)
return false;
js.Syntax.delete(h, id);
js.Syntax.delete(h.__keys__, id);
return true;
}
public function keys():Iterator<K> {
var a = [];
untyped {
js.Syntax.code("for( var key in this.h.__keys__ ) {");
if (h.hasOwnProperty(key))
a.push(h.__keys__[key]);
js.Syntax.code("}");
}
return a.iterator();
}
public function iterator():Iterator<V> {
return untyped {
ref: h,
it: keys(),
hasNext: function() {
return __this__.it.hasNext();
},
next: function() {
var i = __this__.it.next();
return __this__.ref[getId(i)];
}
};
}
@:runtime public inline function keyValueIterator():KeyValueIterator<K, V> {
return new haxe.iterators.MapKeyValueIterator(this);
}
public function copy():ObjectMap<K, V> {
var copied = new ObjectMap();
for (key in keys())
copied.set(key, get(key));
return copied;
}
public function toString():String {
var s = new StringBuf();
s.add("{");
var it = keys();
for (i in it) {
s.add(Std.string(i));
s.add(" => ");
s.add(Std.string(get(i)));
if (it.hasNext())
s.add(", ");
}
s.add("}");
return s.toString();
}
public inline function clear():Void {
h = {__keys__: {}};
}
}

View File

@ -0,0 +1,312 @@
/*
* Copyright (C)2005-2019 Haxe Foundation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
package haxe.ds;
import js.lib.Object;
import haxe.Constraints.IMap;
import haxe.DynamicAccess;
#if (js_es >= 5)
@:coreApi class StringMap<T> implements IMap<String, T> {
var h:Dynamic;
public inline function new() {
h = Object.create(null);
}
public inline function exists(key:String):Bool {
return Object.prototype.hasOwnProperty.call(h, key);
}
public inline function get(key:String):Null<T> {
return h[cast key];
}
public inline function set(key:String, value:T):Void {
h[cast key] = value;
}
public inline function remove(key:String):Bool {
return if (exists(key)) {
js.Syntax.delete(h, key); true;
} else {
false;
}
}
public inline function keys():Iterator<String> {
return new StringMapKeyIterator(h);
}
public inline function iterator():Iterator<T> {
return new StringMapValueIterator(h);
}
public inline function keyValueIterator():KeyValueIterator<String, T> {
return new StringMapKeyValueIterator(h);
}
public inline function copy():StringMap<T> {
return createCopy(h);
}
public inline function clear():Void {
h = Object.create(null);
}
public inline function toString():String {
return stringify(h);
}
// impl
static function createCopy<T>(h:Dynamic):StringMap<T> {
var copy = new StringMap();
js.Syntax.code("for (var key in {0}) {1}[key] = {0}[key]", h, copy.h);
return copy;
}
@:analyzer(no_optimize)
static function stringify(h:Dynamic):String {
var s = "{", first = true;
js.Syntax.code("for (var key in {0}) {", h);
js.Syntax.code("\tif ({0}) {0} = false; else {1} += ',';", first, s);
js.Syntax.code("\t{0} += key + ' => ' + {1}({2}[key]);", s, Std.string, h);
js.Syntax.code("}");
return s + "}";
}
}
private class StringMapKeyIterator {
final h:Dynamic;
final keys:Array<String>;
final length:Int;
var current:Int;
public inline function new(h:Dynamic) {
this.h = h;
keys = Object.keys(h);
length = keys.length;
current = 0;
}
public inline function hasNext():Bool {
return current < length;
}
public inline function next():String {
return keys[current++];
}
}
private class StringMapValueIterator<T> {
final h:Dynamic;
final keys:Array<String>;
final length:Int;
var current:Int;
public inline function new(h:Dynamic) {
this.h = h;
keys = Object.keys(h);
length = keys.length;
current = 0;
}
public inline function hasNext():Bool {
return current < length;
}
public inline function next():T {
return h[cast keys[current++]];
}
}
private class StringMapKeyValueIterator<T> {
final h:Dynamic;
final keys:Array<String>;
final length:Int;
var current:Int;
public inline function new(h:Dynamic) {
this.h = h;
keys = Object.keys(h);
length = keys.length;
current = 0;
}
public inline function hasNext():Bool {
return current < length;
}
public inline function next():{key:String, value:T} {
var key = keys[current++];
return {key: key, value: h[cast key]};
}
}
#else
private class StringMapIterator<T> {
var map:StringMap<T>;
var keys:Array<String>;
var index:Int;
var count:Int;
public inline function new(map:StringMap<T>, keys:Array<String>) {
this.map = map;
this.keys = keys;
this.index = 0;
this.count = keys.length;
}
public inline function hasNext() {
return index < count;
}
public inline function next() {
return map.get(keys[index++]);
}
}
@:coreApi class StringMap<T> implements haxe.Constraints.IMap<String, T> {
private var h:Dynamic;
private var rh:Dynamic;
public inline function new():Void {
h = {};
}
inline function isReserved(key:String):Bool {
return js.Syntax.code("__map_reserved[{0}]", key) != null;
}
public inline function set(key:String, value:T):Void {
if (isReserved(key))
setReserved(key, value);
else
h[cast key] = value;
}
public inline function get(key:String):Null<T> {
if (isReserved(key))
return getReserved(key);
return h[cast key];
}
public inline function exists(key:String):Bool {
if (isReserved(key))
return existsReserved(key);
return h.hasOwnProperty(key);
}
function setReserved(key:String, value:T):Void {
if (rh == null)
rh = {};
rh[cast "$" + key] = value;
}
function getReserved(key:String):Null<T> {
return rh == null ? null : rh[cast "$" + key];
}
function existsReserved(key:String):Bool {
if (rh == null)
return false;
return (cast rh).hasOwnProperty("$" + key);
}
public function remove(key:String):Bool {
if (isReserved(key)) {
key = "$" + key;
if (rh == null || !rh.hasOwnProperty(key))
return false;
js.Syntax.delete(rh, key);
return true;
} else {
if (!h.hasOwnProperty(key))
return false;
js.Syntax.delete(h, key);
return true;
}
}
public function keys():Iterator<String> {
return arrayKeys().iterator();
}
function arrayKeys():Array<String> {
var out = [];
untyped {
js.Syntax.code("for( var key in this.h ) {");
if (h.hasOwnProperty(key))
out.push(key);
js.Syntax.code("}");
}
if (rh != null)
untyped {
js.Syntax.code("for( var key in this.rh ) {");
if (key.charCodeAt(0) == "$".code)
out.push(key.substr(1));
js.Syntax.code("}");
}
return out;
}
public inline function iterator():Iterator<T> {
return new StringMapIterator(this, arrayKeys());
}
@:runtime public inline function keyValueIterator():KeyValueIterator<String, T> {
return new haxe.iterators.MapKeyValueIterator(this);
}
public function copy():StringMap<T> {
var copied = new StringMap();
for (key in keys())
copied.set(key, get(key));
return copied;
}
public function toString():String {
var s = new StringBuf();
s.add("{");
var keys = arrayKeys();
for (i in 0...keys.length) {
var k = keys[i];
s.add(k);
s.add(" => ");
s.add(Std.string(get(k)));
if (i < keys.length - 1)
s.add(", ");
}
s.add("}");
return s.toString();
}
public inline function clear():Void {
h = {};
rh = null;
}
static function __init__():Void {
js.Syntax.code("var __map_reserved = {};");
}
}
#end

View File

@ -0,0 +1,57 @@
/*
* Copyright (C)2005-2019 Haxe Foundation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
package haxe.io;
typedef ArrayBufferViewData = js.lib.ArrayBufferView;
abstract ArrayBufferView(ArrayBufferViewData) {
public var buffer(get, never):haxe.io.Bytes;
public var byteOffset(get, never):Int;
public var byteLength(get, never):Int;
public inline function new(size:Int) {
this = new js.lib.Uint8Array(size);
}
inline function get_byteOffset()
return this.byteOffset;
inline function get_byteLength()
return this.byteLength;
inline function get_buffer():haxe.io.Bytes {
return haxe.io.Bytes.ofData(this.buffer);
}
public inline function sub(begin:Int, ?length:Int) {
return fromData(new js.lib.Uint8Array(this.buffer, begin, length == null ? this.buffer.byteLength - begin : length));
}
public inline function getData():ArrayBufferViewData {
return this;
}
public static inline function fromData(a:ArrayBufferViewData):ArrayBufferView {
return cast a;
}
}

View File

@ -0,0 +1,272 @@
/*
* Copyright (C)2005-2019 Haxe Foundation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
package haxe.io;
@:coreApi
class Bytes {
public var length(default, null):Int;
var b:js.lib.Uint8Array;
var data:js.lib.DataView;
function new(data:BytesData) {
this.length = data.byteLength;
this.b = new js.lib.Uint8Array(data);
untyped {
b.bufferValue = data; // some impl does not return the same instance in .buffer
data.hxBytes = this;
data.bytes = this.b;
}
}
public inline function get(pos:Int):Int {
return b[pos];
}
public inline function set(pos:Int, v:Int):Void {
b[pos] = v;
}
public function blit(pos:Int, src:Bytes, srcpos:Int, len:Int):Void {
if (pos < 0 || srcpos < 0 || len < 0 || pos + len > length || srcpos + len > src.length)
throw Error.OutsideBounds;
if (srcpos == 0 && len == src.b.byteLength)
b.set(src.b, pos);
else
b.set(src.b.subarray(srcpos, srcpos + len), pos);
}
public function fill(pos:Int, len:Int, value:Int):Void {
for (i in 0...len)
set(pos++, value);
}
public function sub(pos:Int, len:Int):Bytes {
if (pos < 0 || len < 0 || pos + len > length)
throw Error.OutsideBounds;
return new Bytes(b.buffer.slice(pos + b.byteOffset, pos + b.byteOffset + len));
}
public function compare(other:Bytes):Int {
var b1 = b;
var b2 = other.b;
var len = (length < other.length) ? length : other.length;
for (i in 0...len)
if (b1[i] != b2[i])
return b1[i] - b2[i];
return length - other.length;
}
inline function initData():Void {
if (data == null)
data = new js.lib.DataView(b.buffer, b.byteOffset, b.byteLength);
}
public function getDouble(pos:Int):Float {
initData();
return data.getFloat64(pos, true);
}
public function getFloat(pos:Int):Float {
initData();
return data.getFloat32(pos, true);
}
public function setDouble(pos:Int, v:Float):Void {
initData();
data.setFloat64(pos, v, true);
}
public function setFloat(pos:Int, v:Float):Void {
initData();
data.setFloat32(pos, v, true);
}
public function getUInt16(pos:Int):Int {
initData();
return data.getUint16(pos, true);
}
public function setUInt16(pos:Int, v:Int):Void {
initData();
data.setUint16(pos, v, true);
}
public function getInt32(pos:Int):Int {
initData();
return data.getInt32(pos, true);
}
public function setInt32(pos:Int, v:Int):Void {
initData();
data.setInt32(pos, v, true);
}
public function getInt64(pos:Int):haxe.Int64 {
return Int64.make(getInt32(pos + 4), getInt32(pos));
}
public function setInt64(pos:Int, v:haxe.Int64):Void {
setInt32(pos, v.low);
setInt32(pos + 4, v.high);
}
public function getString(pos:Int, len:Int, ?encoding:Encoding):String {
if (pos < 0 || len < 0 || pos + len > length)
throw Error.OutsideBounds;
if (encoding == null)
encoding = UTF8;
var s = "";
var b = b;
var i = pos;
var max = pos + len;
switch (encoding) {
case UTF8:
var debug = pos > 0;
// utf8-decode and utf16-encode
while (i < max) {
var c = b[i++];
if (c < 0x80) {
if (c == 0)
break;
s += String.fromCharCode(c);
} else if (c < 0xE0)
s += String.fromCharCode(((c & 0x3F) << 6) | (b[i++] & 0x7F));
else if (c < 0xF0) {
var c2 = b[i++];
s += String.fromCharCode(((c & 0x1F) << 12) | ((c2 & 0x7F) << 6) | (b[i++] & 0x7F));
} else {
var c2 = b[i++];
var c3 = b[i++];
var u = ((c & 0x0F) << 18) | ((c2 & 0x7F) << 12) | ((c3 & 0x7F) << 6) | (b[i++] & 0x7F);
s += String.fromCharCode(u);
}
}
case RawNative:
while (i < max) {
var c = b[i++] | (b[i++] << 8);
s += String.fromCharCode(c);
}
}
return s;
}
@:deprecated("readString is deprecated, use getString instead")
@:noCompletion
public inline function readString(pos:Int, len:Int):String {
return getString(pos, len);
}
public function toString():String {
return getString(0, length);
}
public function toHex():String {
var s = new StringBuf();
var chars = [];
var str = "0123456789abcdef";
for (i in 0...str.length)
chars.push(str.charCodeAt(i));
for (i in 0...length) {
var c = get(i);
s.addChar(chars[c >> 4]);
s.addChar(chars[c & 15]);
}
return s.toString();
}
public inline function getData():BytesData {
return untyped b.bufferValue;
}
public static inline function alloc(length:Int):Bytes {
return new Bytes(new BytesData(length));
}
public static function ofString(s:String, ?encoding:Encoding):Bytes {
if (encoding == RawNative) {
var buf = new js.lib.Uint8Array(s.length << 1);
for (i in 0...s.length) {
var c:Int = StringTools.fastCodeAt(s, i);
buf[i << 1] = c & 0xFF;
buf[(i << 1) | 1] = c >> 8;
}
return new Bytes(buf.buffer);
}
var a = new Array();
// utf16-decode and utf8-encode
var i = 0;
while (i < s.length) {
var c:Int = StringTools.fastCodeAt(s, i++);
// surrogate pair
if (0xD800 <= c && c <= 0xDBFF)
c = (c - 0xD7C0 << 10) | (StringTools.fastCodeAt(s, i++) & 0x3FF);
if (c <= 0x7F)
a.push(c);
else if (c <= 0x7FF) {
a.push(0xC0 | (c >> 6));
a.push(0x80 | (c & 63));
} else if (c <= 0xFFFF) {
a.push(0xE0 | (c >> 12));
a.push(0x80 | ((c >> 6) & 63));
a.push(0x80 | (c & 63));
} else {
a.push(0xF0 | (c >> 18));
a.push(0x80 | ((c >> 12) & 63));
a.push(0x80 | ((c >> 6) & 63));
a.push(0x80 | (c & 63));
}
}
return new Bytes(new js.lib.Uint8Array(a).buffer);
}
public static function ofData(b:BytesData):Bytes {
var hb = untyped b.hxBytes;
if (hb != null)
return hb;
return new Bytes(b);
}
public static function ofHex(s:String):Bytes {
if ((s.length & 1) != 0)
throw "Not a hex string (odd number of digits)";
var a = new Array();
var i = 0;
var len = s.length >> 1;
while (i < len) {
var high = StringTools.fastCodeAt(s, i * 2);
var low = StringTools.fastCodeAt(s, i * 2 + 1);
high = (high & 0xF) + ((high & 0x40) >> 6) * 9;
low = (low & 0xF) + ((low & 0x40) >> 6) * 9;
a.push(((high << 4) | low) & 0xFF);
i++;
}
return new Bytes(new js.lib.Uint8Array(a).buffer);
}
public inline static function fastGet(b:BytesData, pos:Int):Int {
// this requires that we have wrapped it with haxe.io.Bytes beforehand
return untyped b.bytes[pos];
}
}

View File

@ -0,0 +1,127 @@
/*
* Copyright (C)2005-2019 Haxe Foundation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
package haxe.io;
@:coreApi
class BytesBuffer {
var buffer:js.lib.ArrayBuffer;
var view:js.lib.DataView;
var u8:js.lib.Uint8Array;
var pos:Int;
var size:Int;
public var length(get, never):Int;
public function new() {
pos = 0;
size = 0;
}
inline function get_length():Int {
return pos;
}
public function addByte(byte:Int):Void {
if (pos == size)
grow(1);
view.setUint8(pos++, byte);
}
public function add(src:Bytes):Void {
if (pos + src.length > size)
grow(src.length);
if (size == 0)
return;
var sub = new js.lib.Uint8Array(@:privateAccess src.b.buffer, @:privateAccess src.b.byteOffset, src.length);
u8.set(sub, pos);
pos += src.length;
}
public function addString(v:String, ?encoding:Encoding):Void {
add(Bytes.ofString(v, encoding));
}
public function addInt32(v:Int):Void {
if (pos + 4 > size)
grow(4);
view.setInt32(pos, v, true);
pos += 4;
}
public function addInt64(v:haxe.Int64):Void {
if (pos + 8 > size)
grow(8);
view.setInt32(pos, v.low, true);
view.setInt32(pos + 4, v.high, true);
pos += 8;
}
public function addFloat(v:Float):Void {
if (pos + 4 > size)
grow(4);
view.setFloat32(pos, v, true);
pos += 4;
}
public function addDouble(v:Float):Void {
if (pos + 8 > size)
grow(8);
view.setFloat64(pos, v, true);
pos += 8;
}
public function addBytes(src:Bytes, pos:Int, len:Int):Void {
if (pos < 0 || len < 0 || pos + len > src.length)
throw Error.OutsideBounds;
if (this.pos + len > size)
grow(len);
if (size == 0)
return;
var sub = new js.lib.Uint8Array(@:privateAccess src.b.buffer, @:privateAccess src.b.byteOffset + pos, len);
u8.set(sub, this.pos);
this.pos += len;
}
function grow(delta:Int):Void {
var req = pos + delta;
var nsize = size == 0 ? 16 : size;
while (nsize < req)
nsize = (nsize * 3) >> 1;
var nbuf = new js.lib.ArrayBuffer(nsize);
var nu8 = new js.lib.Uint8Array(nbuf);
if (size > 0)
nu8.set(u8);
size = nsize;
buffer = nbuf;
u8 = nu8;
view = new js.lib.DataView(buffer);
}
public function getBytes():Bytes@:privateAccess {
if (size == 0)
return haxe.io.Bytes.alloc(0);
var b = new Bytes(buffer);
b.length = pos;
return b;
}
}

View File

@ -0,0 +1,88 @@
/*
* Copyright (C)2005-2019 Haxe Foundation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
package haxe.io;
typedef Float32ArrayData = js.lib.Float32Array;
@:coreApi
abstract Float32Array(Float32ArrayData) {
public static inline var BYTES_PER_ELEMENT = 4;
public var length(get, never):Int;
public var view(get, never):ArrayBufferView;
public inline function new(elements:Int):Void {
this = new Float32ArrayData(elements);
}
inline function get_length():Int {
return this.length;
}
public inline function get_view():ArrayBufferView {
return ArrayBufferView.fromData(this);
}
@:arrayAccess public inline function get(index:Int):Float {
return this[index];
}
@:arrayAccess public inline function set(index:Int, value:Float):Float {
return this[index] = value;
}
public inline function sub(begin:Int, ?length:Int):Float32Array {
return fromData(this.subarray(begin, length == null ? this.length : begin + length));
}
public inline function subarray(?begin:Int, ?end:Int):Float32Array {
return fromData(this.subarray(begin, end));
}
public inline function getData():Float32ArrayData {
return this;
}
public inline static function fromData(d:Float32ArrayData):Float32Array {
return cast d;
}
public static function fromArray(a:Array<Float>, pos:Int = 0, ?length:Int):Float32Array {
if (length == null)
length = a.length - pos;
if (pos < 0 || length < 0 || pos + length > a.length)
throw Error.OutsideBounds;
if (pos == 0 && length == a.length)
return fromData(new Float32ArrayData(a));
var i = new Float32Array(a.length);
for (idx in 0...length)
i[idx] = a[idx + pos];
return i;
}
public static function fromBytes(bytes:haxe.io.Bytes, bytePos:Int = 0, ?length:Int):Float32Array {
if (length == null)
length = (bytes.length - bytePos) >> 2;
return fromData(new Float32ArrayData(bytes.getData(), bytePos, length));
}
}

View File

@ -0,0 +1,88 @@
/*
* Copyright (C)2005-2019 Haxe Foundation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
package haxe.io;
typedef Float64ArrayData = js.lib.Float64Array;
@:coreApi
abstract Float64Array(Float64ArrayData) {
public static inline var BYTES_PER_ELEMENT = 8;
public var length(get, never):Int;
public var view(get, never):ArrayBufferView;
public inline function new(elements:Int):Void {
this = new Float64ArrayData(elements);
}
inline function get_length():Int {
return this.length;
}
public inline function get_view():ArrayBufferView {
return ArrayBufferView.fromData(this);
}
@:arrayAccess public inline function get(index:Int):Float {
return this[index];
}
@:arrayAccess public inline function set(index:Int, value:Float):Float {
return this[index] = value;
}
public inline function sub(begin:Int, ?length:Int):Float64Array {
return fromData(this.subarray(begin, length == null ? this.length : begin + length));
}
public inline function subarray(?begin:Int, ?end:Int):Float64Array {
return fromData(this.subarray(begin, end));
}
public inline function getData():Float64ArrayData {
return this;
}
public static inline function fromData(d:Float64ArrayData):Float64Array {
return cast d;
}
public static function fromArray(a:Array<Float>, pos:Int = 0, ?length:Int):Float64Array {
if (length == null)
length = a.length - pos;
if (pos < 0 || length < 0 || pos + length > a.length)
throw Error.OutsideBounds;
if (pos == 0 && length == a.length)
return fromData(new Float64ArrayData(a));
var i = new Float64Array(a.length);
for (idx in 0...length)
i[idx] = a[idx + pos];
return i;
}
public static function fromBytes(bytes:haxe.io.Bytes, bytePos:Int = 0, ?length:Int):Float64Array {
if (length == null)
length = (bytes.length - bytePos) >> 3;
return fromData(new Float64ArrayData(bytes.getData(), bytePos, length));
}
}

View File

@ -0,0 +1,88 @@
/*
* Copyright (C)2005-2019 Haxe Foundation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
package haxe.io;
typedef Int32ArrayData = js.lib.Int32Array;
@:coreApi
abstract Int32Array(Int32ArrayData) {
public static inline var BYTES_PER_ELEMENT = 4;
public var length(get, never):Int;
public var view(get, never):ArrayBufferView;
public inline function new(elements:Int) {
this = new Int32ArrayData(elements);
}
inline function get_length():Int {
return this.length;
}
public inline function get_view():ArrayBufferView {
return ArrayBufferView.fromData(this);
}
@:arrayAccess public inline function get(index:Int):Int {
return this[index];
}
@:arrayAccess public inline function set(index:Int, value:Int):Int {
return this[index] = value;
}
public inline function sub(begin:Int, ?length:Int):Int32Array {
return fromData(this.subarray(begin, length == null ? this.length : begin + length));
}
public inline function subarray(?begin:Int, ?end:Int):Int32Array {
return fromData(this.subarray(begin, end));
}
public inline function getData():Int32ArrayData {
return this;
}
public static inline function fromData(d:Int32ArrayData):Int32Array {
return cast d;
}
public static function fromArray(a:Array<Int>, pos:Int = 0, ?length:Int):Int32Array {
if (length == null)
length = a.length - pos;
if (pos < 0 || length < 0 || pos + length > a.length)
throw Error.OutsideBounds;
if (pos == 0 && length == a.length)
return fromData(new Int32ArrayData(a));
var i = new Int32Array(a.length);
for (idx in 0...length)
i[idx] = a[idx + pos];
return i;
}
public static function fromBytes(bytes:haxe.io.Bytes, bytePos:Int = 0, ?length:Int):Int32Array {
if (length == null)
length = (bytes.length - bytePos) >> 2;
return fromData(new Int32ArrayData(bytes.getData(), bytePos, length));
}
}

View File

@ -0,0 +1,88 @@
/*
* Copyright (C)2005-2019 Haxe Foundation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
package haxe.io;
typedef UInt16ArrayData = js.lib.Uint16Array;
@:coreApi
abstract UInt16Array(UInt16ArrayData) {
public static inline var BYTES_PER_ELEMENT = 2;
public var length(get, never):Int;
public var view(get, never):ArrayBufferView;
public inline function new(elements:Int) {
this = new UInt16ArrayData(elements);
}
inline function get_length():Int {
return this.length;
}
public inline function get_view():ArrayBufferView {
return ArrayBufferView.fromData(this);
}
@:arrayAccess public inline function get(index:Int):Int {
return this[index];
}
@:arrayAccess public inline function set(index:Int, value:Int):Int {
return this[index] = value;
}
public inline function sub(begin:Int, ?length:Int):UInt16Array {
return fromData(this.subarray(begin, length == null ? this.length : begin + length));
}
public inline function subarray(?begin:Int, ?end:Int):UInt16Array {
return fromData(this.subarray(begin, end));
}
public inline function getData():UInt16ArrayData {
return this;
}
public static inline function fromData(d:UInt16ArrayData):UInt16Array {
return cast d;
}
public static function fromArray(a:Array<Int>, pos:Int = 0, ?length:Int):UInt16Array {
if (length == null)
length = a.length - pos;
if (pos < 0 || length < 0 || pos + length > a.length)
throw Error.OutsideBounds;
if (pos == 0 && length == a.length)
return fromData(new UInt16ArrayData(a));
var i = new UInt16Array(a.length);
for (idx in 0...length)
i[idx] = a[idx + pos];
return i;
}
public static function fromBytes(bytes:haxe.io.Bytes, bytePos:Int = 0, ?length:Int):UInt16Array {
if (length == null)
length = (bytes.length - bytePos) >> 1;
return fromData(new UInt16ArrayData(bytes.getData(), bytePos, length));
}
}

View File

@ -0,0 +1,88 @@
/*
* Copyright (C)2005-2019 Haxe Foundation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
package haxe.io;
typedef UInt32ArrayData = js.lib.Uint32Array;
@:coreApi
abstract UInt32Array(UInt32ArrayData) {
public static inline var BYTES_PER_ELEMENT = 4;
public var length(get, never):Int;
public var view(get, never):ArrayBufferView;
public inline function new(elements:Int) {
this = new UInt32ArrayData(elements);
}
inline function get_length():Int {
return this.length;
}
public inline function get_view():ArrayBufferView {
return ArrayBufferView.fromData(this);
}
@:arrayAccess public inline function get(index:Int):UInt {
return this[index];
}
@:arrayAccess public inline function set(index:Int, value:UInt):UInt {
return this[index] = value;
}
public inline function sub(begin:Int, ?length:Int):UInt32Array {
return fromData(this.subarray(begin, length == null ? this.length : begin + length));
}
public inline function subarray(?begin:Int, ?end:Int):UInt32Array {
return fromData(this.subarray(begin, end));
}
public inline function getData():UInt32ArrayData {
return this;
}
public static inline function fromData(d:UInt32ArrayData):UInt32Array {
return cast d;
}
public static function fromArray(a:Array<UInt>, pos:Int = 0, ?length:Int):UInt32Array {
if (length == null)
length = a.length - pos;
if (pos < 0 || length < 0 || pos + length > a.length)
throw Error.OutsideBounds;
if (pos == 0 && length == a.length)
return fromData(new UInt32ArrayData(a));
var i = new UInt32Array(a.length);
for (idx in 0...length)
i[idx] = a[idx + pos];
return i;
}
public static function fromBytes(bytes:haxe.io.Bytes, bytePos:Int = 0, ?length:Int):UInt32Array {
if (length == null)
length = (bytes.length - bytePos) >> 2;
return fromData(new UInt32ArrayData(bytes.getData(), bytePos, length));
}
}

View File

@ -0,0 +1,88 @@
/*
* Copyright (C)2005-2019 Haxe Foundation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
package haxe.io;
typedef UInt8ArrayData = js.lib.Uint8Array;
@:coreApi
abstract UInt8Array(UInt8ArrayData) {
public static inline var BYTES_PER_ELEMENT = 1;
public var length(get, never):Int;
public var view(get, never):ArrayBufferView;
public inline function new(elements:Int) {
this = new UInt8ArrayData(elements);
}
inline function get_length():Int {
return this.length;
}
public inline function get_view():ArrayBufferView {
return ArrayBufferView.fromData(this);
}
@:arrayAccess public inline function get(index:Int):Int {
return this[index];
}
@:arrayAccess public inline function set(index:Int, value:Int):Int {
return this[index] = value;
}
public inline function sub(begin:Int, ?length:Int):UInt8Array {
return fromData(this.subarray(begin, length == null ? this.length : begin + length));
}
public inline function subarray(?begin:Int, ?end:Int):UInt8Array {
return fromData(this.subarray(begin, end));
}
public inline function getData():UInt8ArrayData {
return this;
}
public static inline function fromData(d:UInt8ArrayData):UInt8Array {
return cast d;
}
public static function fromArray(a:Array<Int>, pos:Int = 0, ?length:Int):UInt8Array {
if (length == null)
length = a.length - pos;
if (pos < 0 || length < 0 || pos + length > a.length)
throw Error.OutsideBounds;
if (pos == 0 && length == a.length)
return fromData(new UInt8ArrayData(a));
var i = new UInt8Array(a.length);
for (idx in 0...length)
i[idx] = a[idx + pos];
return i;
}
public static function fromBytes(bytes:haxe.io.Bytes, bytePos:Int = 0, ?length:Int):UInt8Array {
if (length == null)
length = bytes.length - bytePos;
return fromData(new UInt8ArrayData(bytes.getData(), bytePos, length));
}
}