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,386 @@
/*
* 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 final class Array<T> {
private var __a:neko.NativeArray<T>;
public var length(default, null):Int;
public function new():Void {
this.__a = neko.NativeArray.alloc(0);
this.length = 0;
}
private static function new1<T>(a:neko.NativeArray<T>, l:Int):Array<T> {
var inst = new Array<T>();
inst.__a = a;
inst.length = l;
return inst;
}
public function concat(a:Array<T>):Array<T> {
var a1 = this.__a;
var a2 = a.__a;
var s1 = this.length;
var s2 = a.length;
var a = neko.NativeArray.alloc(s1 + s2);
neko.NativeArray.blit(a, 0, a1, 0, s1);
neko.NativeArray.blit(a, s1, a2, 0, s2);
return new1(a, s1 + s2);
}
public function copy():Array<T> {
return new1(neko.NativeArray.sub(this.__a, 0, this.length), this.length);
}
public inline function iterator():haxe.iterators.ArrayIterator<T> {
return new haxe.iterators.ArrayIterator(this);
}
public inline function keyValueIterator():ArrayKeyValueIterator<T> {
return new ArrayKeyValueIterator(this);
}
public function insert(pos:Int, x:T):Void {
var l = this.length;
if (pos < 0) {
pos = l + pos;
if (pos < 0)
pos = 0;
}
if (pos > l)
pos = l;
this.__grow(l + 1);
var a = this.__a;
neko.NativeArray.blit(a, pos + 1, a, pos, l - pos);
a[pos] = x;
}
public function join(sep:String):String {
var s = new StringBuf();
var a = this.__a;
var max = this.length - 1;
for (p in 0...this.length) {
s.add(a[p]);
if (p != max)
s.add(sep);
}
return s.toString();
}
static var __hx_toString_depth = 0;
public function toString():String {
if (__hx_toString_depth >= 5) {
return "...";
}
var s = new StringBuf();
s.add("[");
var it = iterator();
__hx_toString_depth++;
try {
for (i in it) {
s.add(i);
if (it.hasNext())
s.addChar(",".code);
}
__hx_toString_depth--;
} catch (e:Dynamic) {
__hx_toString_depth--;
neko.Lib.rethrow(e);
}
s.add("]");
return s.toString();
}
public function pop():Null<T> {
if (this.length == 0)
return null;
this.length -= 1;
var x = this.__a[this.length];
this.__a[this.length] = null;
return x;
}
public function push(x:T):Int {
var l = this.length;
this.__grow(l + 1);
this.__a[l] = x;
return l + 1;
}
public function unshift(x:T):Void {
var l = this.length;
this.__grow(l + 1);
var a = this.__a;
neko.NativeArray.blit(a, 1, a, 0, l);
a[0] = x;
}
public function remove(x:T):Bool {
var i = 0;
var l = this.length;
var a = this.__a;
while (i < l) {
if (a[i] == x) {
neko.NativeArray.blit(a, i, a, i + 1, l - i - 1);
l -= 1;
this.length = l;
a[l] = null;
return true;
}
i += 1;
}
return false;
}
public function contains(x:T):Bool {
var i = 0;
var l = this.length;
var a = this.__a;
while (i < l) {
if (a[i] == x) {
return true;
}
i += 1;
}
return false;
}
public function indexOf(x:T, ?fromIndex:Int):Int {
var len = length;
var i:Int = (fromIndex != null) ? fromIndex : 0;
var a = __a;
if (i < 0) {
i += len;
if (i < 0)
i = 0;
}
while (i < len) {
if (a[i] == x)
return i;
i++;
}
return -1;
}
public function lastIndexOf(x:T, ?fromIndex:Int):Int {
var len = length;
var i:Int = (fromIndex != null) ? fromIndex : len - 1;
var a = __a;
if (i >= len)
i = len - 1;
else if (i < 0)
i += len;
while (i >= 0) {
if (a[i] == x)
return i;
i--;
}
return -1;
}
public function reverse():Void {
var i = 0;
var l = this.length;
var a = this.__a;
var half = l >> 1;
l -= 1;
while (i < half) {
var tmp = a[i];
a[i] = a[l - i];
a[l - i] = tmp;
i += 1;
}
}
public function shift():Null<T> {
var l = this.length;
if (l == 0)
return null;
var a = this.__a;
var x = a[0];
l -= 1;
neko.NativeArray.blit(a, 0, a, 1, l);
a[l] = null;
this.length = l;
return x;
}
public function slice(pos:Int, ?end:Int):Array<T> {
if (pos < 0) {
pos = this.length + pos;
if (pos < 0)
pos = 0;
}
if (end == null)
end = this.length;
else if (end < 0)
end = this.length + end;
if (end > this.length)
end = this.length;
var len = end - pos;
if (len < 0)
return new Array();
return new1(neko.NativeArray.sub(this.__a, pos, len), len);
}
public function sort(f:T->T->Int):Void {
var a = this.__a;
var i = 0;
var l = this.length;
while (i < l) {
var swap = false;
var j = 0;
var max = l - i - 1;
while (j < max) {
if (f(a[j], a[j + 1]) > 0) {
var tmp = a[j + 1];
a[j + 1] = a[j];
a[j] = tmp;
swap = true;
}
j += 1;
}
if (!swap)
break;
i += 1;
}
}
public function splice(pos:Int, len:Int):Array<T> {
if (len < 0)
return new Array();
if (pos < 0) {
pos = this.length + pos;
if (pos < 0)
pos = 0;
}
if (pos > this.length) {
pos = 0;
len = 0;
} else if (pos + len > this.length) {
len = this.length - pos;
if (len < 0)
len = 0;
}
var a = this.__a;
var ret = new1(neko.NativeArray.sub(a, pos, len), len);
var end = pos + len;
neko.NativeArray.blit(a, pos, a, end, this.length - end);
this.length -= len;
while (--len >= 0)
a[this.length + len] = null;
return ret;
}
public inline function map<S>(f:T->S):Array<S> {
var l = length;
var ret = new1(neko.NativeArray.alloc(l), l);
for (i in 0...l) {
ret[i] = f(this[i]);
}
return ret;
}
public inline function filter(f:T->Bool):Array<T> {
var ret = [];
for (elt in this)
if (f(elt))
ret.push(elt);
return ret;
}
public function resize(len:Int):Void {
if (length < len) {
__set(len - 1, null);
} else if (length > len) {
for (i in len...length) {
__a[i] = null;
}
this.length = len;
}
}
/* NEKO INTERNAL */
private function __get(pos:Int):T {
return this.__a[pos];
}
private function __set(pos:Int, v:T):T {
var a = this.__a;
if (this.length <= pos) {
var l = pos + 1;
var dlen = l - neko.NativeArray.length(a);
if (dlen > 0) {
if (dlen == 1) {
this.__grow(l);
a = this.__a;
} else {
a = neko.NativeArray.alloc(l);
neko.NativeArray.blit(a, 0, this.__a, 0, this.length);
this.__a = a;
}
}
this.length = l;
}
a[pos] = v;
return v;
}
private function __grow(l:Int):Void {
var a = this.__a;
var sz = neko.NativeArray.length(a);
if (sz >= l) {
this.length = l;
return;
}
var big = (sz * 3) >> 1;
if (big < l)
big = l;
var a2 = neko.NativeArray.alloc(big);
neko.NativeArray.blit(a2, 0, a, 0, this.length);
this.__a = a2;
this.length = l;
}
private function __neko():neko.NativeArray<T> {
var a = this.__a;
var sz = neko.NativeArray.length(a);
if (sz != this.length) {
a = neko.NativeArray.sub(a, 0, this.length);
this.__a = a;
}
return a;
}
#if !(macro || interp)
static function __init__():Void {
try {
var msort:Dynamic = neko.Lib.load("std", "merge_sort", 3);
untyped Array.prototype.sort = function(cmp) msort(__this__.__a, __this__.length, cmp);
} catch (e:Dynamic) {}
}
#end
}

View File

@ -0,0 +1,141 @@
/*
* 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 neko.Lib;
@:coreApi final class Date {
private var __t:Dynamic;
public function new(year:Int, month:Int, day:Int, hour:Int, min:Int, sec:Int):Void {
__t = date_set_day(0, year, month + 1, day);
__t = date_set_hour(__t, hour, min, sec);
}
public function getTime():Float {
return int32_to_float(__t) * 1000;
}
public function getFullYear():Int {
return date_get_day(__t).y;
}
public function getMonth():Int {
return date_get_day(__t).m - 1;
}
public function getDate():Int {
return date_get_day(__t).d;
}
public function getHours():Int {
return date_get_hour(__t).h;
}
public function getMinutes():Int {
return date_get_hour(__t).m;
}
public function getSeconds():Int {
return date_get_hour(__t).s;
}
public function getDay():Int {
return Std.parseInt(new String(date_format(__t, untyped "%w".__s)));
}
public function getUTCFullYear():Int {
return date_get_utc_day(__t).y;
}
public function getUTCMonth():Int {
return date_get_utc_day(__t).m - 1;
}
public function getUTCDate():Int {
return date_get_utc_day(__t).d;
}
public function getUTCHours():Int {
return date_get_utc_hour(__t).h;
}
public function getUTCMinutes():Int {
return date_get_utc_hour(__t).m;
}
public function getUTCSeconds():Int {
return date_get_utc_hour(__t).s;
}
public function getUTCDay():Int {
return Std.parseInt(new String(date_utc_format(__t, untyped "%w".__s)));
}
public function getTimezoneOffset():Int {
return -date_get_tz(__t);
}
@:keep public function toString():String {
return new String(date_format(__t, null));
}
public static function now():Date {
return new1(date_now());
}
public static function fromTime(t:Float):Date {
t /= 1000;
var i1 = untyped __dollar__int((t % 65536));
var i2 = untyped __dollar__int(t / 65536);
var i = int32_add(i1, int32_shl(i2, 16));
return new1(i);
}
public static function fromString(s:String):Date {
return new1(date_new(untyped s.__s));
}
private static function new1(t:Dynamic):Date {
var d = new Date(2005, 1, 1, 0, 0, 0);
d.__t = t;
return d;
}
static var date_new = Lib.load("std", "date_new", 1);
static var date_now = Lib.load("std", "date_now", 0);
static var date_format = Lib.load("std", "date_format", 2);
static var date_utc_format = Lib.load("std", "date_utc_format", 2);
static var date_set_hour = Lib.load("std", "date_set_hour", 4);
static var date_set_day = Lib.load("std", "date_set_day", 4);
static var date_get_day:Dynamic->{y: Int, m: Int, d: Int} = Lib.load("std", "date_get_day", 1);
static var date_get_hour:Dynamic->{h: Int, m: Int, s: Int} = Lib.load("std", "date_get_hour", 1);
static var date_get_utc_day:Dynamic->{y: Int, m: Int, d: Int} = Lib.load("std", "date_get_utc_day", 1);
static var date_get_utc_hour:Dynamic->{h: Int, m: Int, s: Int} = Lib.load("std", "date_get_utc_hour", 1);
static var date_get_tz = Lib.load("std", "date_get_tz", 1);
static var int32_to_float = Lib.load("std", "int32_to_float", 1);
static var int32_add = Lib.load("std", "int32_add", 2);
static var int32_shl = Lib.load("std", "int32_shl", 2);
@:keep static function __string():String {
return untyped "Date".__s;
}
}

View File

@ -0,0 +1,209 @@
/*
* 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 final class EReg {
var r:Dynamic;
var last:String;
var global:Bool;
public function new(r:String, opt:String):Void {
var a = opt.split("g");
global = a.length > 1;
if (global)
opt = a.join("");
this.r = regexp_new_options(untyped r.__s, untyped opt.__s);
}
public function match(s:String):Bool {
var p = regexp_match(r, untyped s.__s, 0, s.length);
if (p)
last = s;
else
last = null;
return p;
}
public function matched(n:Int):String {
var m = regexp_matched(r, n);
return (m == null) ? null : new String(m);
}
public function matchedLeft():String {
var p = regexp_matched_pos(r, 0);
return last.substr(0, p.pos);
}
public function matchedRight():String {
var p = regexp_matched_pos(r, 0);
var sz = p.pos + p.len;
return last.substr(sz, last.length - sz);
}
public function matchedPos():{pos:Int, len:Int} {
return regexp_matched_pos(r, 0);
}
public function matchSub(s:String, pos:Int, len:Int = -1):Bool {
var p = regexp_match(r, untyped s.__s, pos, len < 0 ? s.length - pos : len);
if (p)
last = s;
else
last = null;
return p;
}
public function split(s:String):Array<String> {
var pos = 0;
var len = s.length;
var a = new Array();
var first = true;
do {
if (!regexp_match(r, untyped s.__s, pos, len))
break;
var p = regexp_matched_pos(r, 0);
if (p.len == 0 && !first) {
if (p.pos == s.length)
break;
p.pos += 1;
}
a.push(s.substr(pos, p.pos - pos));
var tot = p.pos + p.len - pos;
pos += tot;
len -= tot;
first = false;
} while (global);
a.push(s.substr(pos, len));
return a;
}
public function replace(s:String, by:String):String {
var b = new StringBuf();
var pos = 0;
var len = s.length;
var a = by.split("$");
var first = true;
do {
if (!regexp_match(r, untyped s.__s, pos, len))
break;
var p = regexp_matched_pos(r, 0);
if (p.len == 0 && !first) {
if (p.pos == s.length)
break;
p.pos += 1;
}
b.addSub(s, pos, p.pos - pos);
if (a.length > 0)
b.add(a[0]);
var i = 1;
while (i < a.length) {
var k = a[i];
var c = k.charCodeAt(0);
// 1...9
if (c >= 49 && c <= 57) {
var p = try regexp_matched_pos(r, Std.int(c) - 48) catch (e:String) null;
if (p == null) {
b.add("$");
b.add(k);
} else {
if (p.pos >= 0)
b.addSub(s, p.pos, p.len);
b.addSub(k, 1, k.length - 1);
}
} else if (c == null) {
b.add("$");
i++;
var k2 = a[i];
if (k2 != null && k2.length > 0)
b.add(k2);
} else
b.add("$" + k);
i++;
}
var tot = p.pos + p.len - pos;
pos += tot;
len -= tot;
first = false;
} while (global);
b.addSub(s, pos, len);
return b.toString();
}
// public function map( s : String, f : EReg -> String ) : String {
// var b = new StringBuf();
// var pos = 0;
// var len = s.length;
// var first = true;
// last = s;
// do {
// if( !regexp_match(r,untyped s.__s,pos,len) )
// break;
// var p = regexp_matched_pos(r,0);
// if( p.len == 0 && !first ) {
// if( p.pos == s.length )
// break;
// p.pos += 1;
// }
// b.addSub(s,pos,p.pos-pos);
// b.add(f(this));
// var tot = p.pos + p.len - pos;
// pos += tot;
// len -= tot;
// first = false;
// } while( global );
// b.addSub(s,pos,len);
// return b.toString();
// }
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 = regexp_matched_pos(r, 0);
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 (global);
if (!global && offset > 0 && offset < s.length)
buf.add(s.substr(offset));
return buf.toString();
}
public static function escape(s:String):String {
return escapeRegExpRe.map(s, function(r) return "\\" + r.matched(0));
}
static var escapeRegExpRe = ~/[\[\]{}()*+?.\\\^$|]/g;
static var regexp_new_options = neko.Lib.load("regexp", "regexp_new_options", 2);
static var regexp_match = neko.Lib.load("regexp", "regexp_match", 4);
static var regexp_matched = neko.Lib.load("regexp", "regexp_matched", 2);
static var regexp_matched_pos:Dynamic->Int->{pos: Int, len: Int} = neko.Lib.load("regexp", "regexp_matched_pos", 2);
}

View File

@ -0,0 +1,110 @@
/*
* 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 neko.Lib;
@:native("Math")
@:keep
private class MathImpl {
static var __rnd:Dynamic;
static var _rand_float = Lib.load("std", "random_float", 1);
static var _rand_int = Lib.load("std", "random_int", 2);
public static function min(a:Float, b:Float):Float {
return if (a < b) a else if (untyped $isnan(a)) a else b;
}
public static function max(a:Float, b:Float):Float {
return if (a < b) b else if (untyped $isnan(b)) b else a;
}
public static function random():Float {
return _rand_float(__rnd);
}
public static function isNaN(f:Float):Bool {
return untyped __dollar__isnan(f);
}
public static function isFinite(f:Float):Bool {
return !untyped (__dollar__isinfinite(f) || __dollar__isnan(f));
}
static function __init__():Void {
var M:Dynamic = MathImpl;
__rnd = Lib.load("std", "random_new", 0)();
M.PI = Lib.load("std", "math_pi", 0)();
M.NaN = 0.0 / 0.0;
M.POSITIVE_INFINITY = 1.0 / 0.0;
M.NEGATIVE_INFINITY = -M.POSITIVE_INFINITY;
M.abs = Lib.load("std", "math_abs", 1);
M.sin = Lib.load("std", "math_sin", 1);
M.cos = Lib.load("std", "math_cos", 1);
M.atan2 = Lib.load("std", "math_atan2", 2);
M.tan = Lib.load("std", "math_tan", 1);
M.exp = Lib.load("std", "math_exp", 1);
M.log = Lib.load("std", "math_log", 1);
M.sqrt = Lib.load("std", "math_sqrt", 1);
M.round = Lib.load("std", "math_round", 1);
M.floor = Lib.load("std", "math_floor", 1);
M.ceil = Lib.load("std", "math_ceil", 1);
M.atan = Lib.load("std", "math_atan", 1);
M.asin = Lib.load("std", "math_asin", 1);
M.acos = Lib.load("std", "math_acos", 1);
M.pow = Lib.load("std", "math_pow", 2);
M.fceil = try Lib.load("std", "math_fceil", 1) catch (e:Dynamic) M.ceil;
M.ffloor = try Lib.load("std", "math_ffloor", 1) catch (e:Dynamic) M.floor;
M.fround = try Lib.load("std", "math_fround", 1) catch (e:Dynamic) M.round;
}
}
@:coreApi
extern final class Math {
public static var PI(default, null):Float;
public static var NaN(default, null):Float;
public static var POSITIVE_INFINITY(default, null):Float;
public static var NEGATIVE_INFINITY(default, null):Float;
public static function min(a:Float, b:Float):Float;
public static function max(a:Float, b:Float):Float;
public static function abs(v:Float):Float;
public static function sin(v:Float):Float;
public static function cos(v:Float):Float;
public static function atan2(y:Float, x:Float):Float;
public static function tan(v:Float):Float;
public static function exp(v:Float):Float;
public static function log(v:Float):Float;
public static function sqrt(v:Float):Float;
public static function round(v:Float):Int;
public static function floor(v:Float):Int;
public static function ceil(v:Float):Int;
public static function atan(v:Float):Float;
public static function asin(v:Float):Float;
public static function acos(v:Float):Float;
public static function pow(v:Float, exp:Float):Float;
public static function fround(v:Float):Float;
public static function ffloor(v:Float):Float;
public static function fceil(v:Float):Float;
public static function random():Float;
public static function isNaN(f:Float):Bool;
public static function isFinite(f:Float):Bool;
}

View File

@ -0,0 +1,136 @@
/*
* 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 {
public static function hasField(o:Dynamic, field:String):Bool
untyped {
return $typeof(o) == $tobject && $objfield(o, $fasthash(field.__s));
}
public inline static function field(o:Dynamic, field:String):Dynamic
untyped {
return if ($typeof(o) != $tobject) null else $objget(o, $fasthash(field.__s));
}
public inline static function setField(o:Dynamic, field:String, value:Dynamic):Void
untyped {
if ($typeof(o) == $tobject)
$objset(o, $hash(field.__s), value);
}
public static inline function getProperty(o:Dynamic, field:String):Dynamic
untyped {
var tmp;
return if ($typeof(o) != $tobject) null else if (o.__properties__ != null
&& (tmp = $objget(o.__properties__, $fasthash("get_".__s + field.__s))) != null) $call($objget(o, $fasthash(tmp)), o, $array()) else $objget(o,
$fasthash(field.__s));
}
public static inline function setProperty(o:Dynamic, field:String, value:Dynamic):Void
untyped {
if ($typeof(o) == $tobject) {
var tmp;
if (o.__properties__ != null && (tmp = $objget(o.__properties__, $fasthash("set_".__s + field.__s))) != null)
$call($objget(o, $fasthash(tmp)), o, $array(value))
else
$objset(o, $hash(field.__s), value);
}
}
public static function callMethod(o:Dynamic, func:haxe.Constraints.Function, args:Array<Dynamic>):Dynamic
untyped {
var a = args.__neko();
// pad missing args with null's
var n = $nargs(func);
if (n > $asize(a)) {
var a2 = $amake(n);
$ablit(a2, 0, a, 0, $asize(a));
a = a2;
}
return $call(func, o, a);
}
public static function fields(o:Dynamic):Array<String>
untyped {
if ($typeof(o) != $tobject)
return new Array<String>();
else {
var a:neko.NativeArray<Int> = $objfields(o);
var i = 0;
var hasid = false;
var l = $asize(a);
while (i < l) {
var fid = a[i];
if (fid == -190054693)
hasid = true; // $hash("__id__")
a[i] = new String($field(fid));
i++;
}
var a:Array<String> = Array.new1(a, l);
if (hasid)
a.remove("__id__");
return a;
}
}
public static function isFunction(f:Dynamic):Bool
untyped {
return $typeof(f) == $tfunction;
}
public inline static function compare<T>(a:T, b:T):Int {
return untyped $compare(a, b);
}
public inline static function compareMethods(f1:Dynamic, f2:Dynamic):Bool {
return same_closure(f1, f2);
}
public static function isObject(v:Dynamic):Bool
untyped {
return $typeof(v) == $tobject && v.__enum__ == null;
}
public static function isEnumValue(v:Dynamic):Bool
untyped {
return $typeof(v) == $tobject && v.__enum__ != null;
}
public inline static function deleteField(o:Dynamic, field:String):Bool
untyped {
return $objremove(o, $fasthash(field.__s));
}
public static function copy<T>(o:Null<T>):Null<T> {
if (o == null)
return null;
return untyped $new(o);
}
@:overload(function(f:Array<Dynamic>->Void):Dynamic {})
public static function makeVarArgs(f:Array<Dynamic>->Dynamic):Dynamic {
return untyped $varargs(function(a) {
return f(Array.new1(a, $asize(a)));
});
}
static var same_closure = try neko.Lib.load("std", "same_closure", 2) catch (e:Dynamic) function(f1, f2) return f1 == f2;
}

View File

@ -0,0 +1,94 @@
/*
* 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 Std {
@:ifFeature("typed_cast")
@: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 function isOfType(v:Dynamic, t:Dynamic):Bool {
return untyped neko.Boot.__instanceof(v, t);
}
public static function downcast<T:{}, S:T>(value:T, c:Class<S>):S {
return Std.isOfType(value, c) ? cast value : null;
}
@:deprecated('Std.instance() is deprecated. Use Std.downcast() instead.')
public static function instance<T:{}, S:T>(value:T, c:Class<S>):S {
return inline downcast(value, c);
}
public static function string(s:Dynamic):String {
return new String(untyped __dollar__string(s));
}
public static function int(x:Float):Int {
if (x < 0)
return Math.ceil(x);
return Math.floor(x);
}
public static function parseInt(x:String):Null<Int>
untyped {
var t = __dollar__typeof(x);
if (t == __dollar__tint)
return x;
if (t == __dollar__tfloat)
return __dollar__int(x);
if (t != __dollar__tobject)
return null;
return __dollar__int(x.__s);
}
public static function parseFloat(x:String):Float
untyped {
if (x == null)
return Math.NaN;
var t = __dollar__float(x.__s);
if (t == null)
t = Math.NaN;
return t;
}
public static function random(x:Int):Int {
return untyped Math._rand_int(Math.__rnd, x);
}
static function __init__():Void
untyped {
Int = {__name__: ["Int"]};
Float = {__name__: ["Float"]};
Bool = {__ename__: ["Bool"]};
Dynamic = {__name__: ["Dynamic"]};
Class = {__name__: ["Class"]};
Enum = {};
var cl = neko.Boot.__classes;
cl.Int = Int;
cl.Float = Float;
cl.Bool = Bool;
cl.Dynamic = Dynamic;
cl.Class = Class;
cl.Enum = Enum;
}
}

View File

@ -0,0 +1,221 @@
/*
* 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 final class String {
static var __is_String = true;
private static var __split:Dynamic = neko.Lib.load("std", "string_split", 2);
static function __init__():Void {
__is_String = true;
}
public var length(default, null):Int;
public function new(string:String):Void {
untyped {
if (__dollar__typeof(string) != __dollar__tstring)
string = __dollar__string(string);
this.__s = string;
this.length = __dollar__ssize(string);
}
}
public function charAt(index:Int):String {
untyped {
try {
var s = __dollar__smake(1);
__dollar__sset(s, 0, __dollar__sget(this.__s, index));
return new String(s);
} catch (e:Dynamic) {
return "";
}
}
}
public function charCodeAt(index:Int):Null<Int> {
untyped {
return __dollar__sget(this.__s, index);
}
}
public function indexOf(str:String, ?startIndex:Int):Int {
untyped {
var l = __dollar__ssize(this.__s);
if (startIndex == null || startIndex < -l)
startIndex = 0;
if (str == '' && startIndex >= l) {
return l;
}
if (startIndex > l)
return -1;
if (__dollar__ssize(str.__s) == 0)
return startIndex < 0 ? l + startIndex : startIndex;
var p = try __dollar__sfind(this.__s, startIndex, str.__s) catch (e:Dynamic) null;
if (p == null)
return -1;
return p;
}
}
public function lastIndexOf(str:String, ?startIndex:Int):Int {
untyped {
var last = -1;
var l = __dollar__ssize(this.__s);
if (startIndex == null)
startIndex = l;
if (__dollar__ssize(str.__s) == 0)
return startIndex > l ? l : startIndex;
while (true) {
var p = try __dollar__sfind(this.__s, last + 1, str.__s) catch (e:Dynamic) null;
if (p == null || p > startIndex)
return last;
last = p;
}
}
}
public function split(delimiter:String):Array<String> {
untyped {
var l = __split(this.__s, delimiter.__s);
var a = new Array<String>();
if (l == null) {
a.push("");
return a;
}
do {
a.push(new String(l[0]));
l = l[1];
} while (l != null);
return a;
}
}
public function substr(pos:Int, ?len:Int):String {
if (len == 0)
return "";
var sl = length;
if (len == null)
len = sl;
if (pos == null)
pos = 0;
if (pos != 0 && len < 0) {
return "";
}
if (pos < 0) {
pos = sl + pos;
if (pos < 0)
pos = 0;
} else if (len < 0) {
len = sl + len - pos;
}
if (pos + len > sl) {
len = sl - pos;
}
if (pos < 0 || len <= 0)
return "";
return new String(untyped __dollar__ssub(this.__s, pos, len));
}
public function substring(startIndex:Int, ?endIndex:Int):String {
if (endIndex == null) {
endIndex = length;
} else if (endIndex < 0) {
endIndex = 0;
} else if (endIndex > length) {
endIndex = length;
}
if (startIndex < 0) {
startIndex = 0;
} else if (startIndex > length) {
startIndex = length;
}
if (startIndex > endIndex) {
var tmp = startIndex;
startIndex = endIndex;
endIndex = tmp;
}
return substr(startIndex, endIndex - startIndex);
}
public function toLowerCase():String {
untyped {
var s = this.__s;
var l = this.length;
var s2 = __dollar__scopy(s);
var i = 0;
while (i < l) {
var c = __dollar__sget(s, i);
if (c >= 65 && c <= 90)
__dollar__sset(s2, i, c - 65 + 97);
i++;
}
return new String(s2);
}
}
public function toUpperCase():String {
untyped {
var s = this.__s;
var l = this.length;
var s2 = __dollar__scopy(s);
var i = 0;
while (i < l) {
var c = __dollar__sget(s, i);
if (c >= 97 && c <= 122)
__dollar__sset(s2, i, c - 97 + 65);
i++;
}
return new String(s2);
}
}
public function toString():String {
return this;
}
/* NEKO INTERNALS */
private function __compare(o:String):Int {
return untyped __dollar__compare(this.__s, o.__s);
}
private function __add(s:Dynamic):String {
return new String(untyped this.__s + __dollar__string(s));
}
private function __radd(s:Dynamic):String {
return new String(untyped __dollar__string(s) + this.__s);
}
public static function fromCharCode(code:Int):String
untyped {
var s = __dollar__smake(1);
__dollar__sset(s, 0, code);
return new String(s);
}
}

View File

@ -0,0 +1,58 @@
/*
* 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 StringBuf {
private var b:Dynamic;
public var length(get, never):Int;
public function new():Void {
b = __make();
}
function get_length():Int {
return __get_length == null ? untyped __dollar__ssize(__to_string(b)) : __get_length(b);
}
public inline function add<T>(x:T):Void {
__add(b, x);
}
public inline function addSub(s:String, pos:Int, ?len:Int):Void {
__add_sub(b, untyped s.__s, pos, len == null ? s.length - pos : len);
}
public inline function addChar(c:Int):Void
untyped {
__add_char(b, c);
}
public inline function toString():String {
return new String(__to_string(b));
}
static var __make:Dynamic = neko.Lib.load("std", "buffer_new", 0);
static var __add:Dynamic = neko.Lib.load("std", "buffer_add", 2);
static var __add_char:Dynamic = neko.Lib.load("std", "buffer_add_char", 2);
static var __add_sub:Dynamic = neko.Lib.load("std", "buffer_add_sub", 4);
static var __to_string:Dynamic = neko.Lib.load("std", "buffer_string", 1);
static var __get_length:Dynamic = try neko.Lib.load("std", "buffer_get_length", 1) catch (e:Dynamic) null;
}

View File

@ -0,0 +1,200 @@
/*
* 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.SysTools;
@:coreApi class Sys {
public static function print( v : Dynamic ) : Void {
untyped __dollar__print(v);
}
public static function println( v : Dynamic ) : Void {
untyped __dollar__print(v,"\n");
}
public static function getChar( echo : Bool ) : Int {
return getch(echo);
}
public static function stdin() : haxe.io.Input {
return untyped new sys.io.FileInput(file_stdin());
}
public static function stdout() : haxe.io.Output {
return untyped new sys.io.FileOutput(file_stdout());
}
public static function stderr() : haxe.io.Output {
return untyped new sys.io.FileOutput(file_stderr());
}
public static function args() : Array<String> untyped {
var a = __dollar__loader.args;
if( __dollar__typeof(a) != __dollar__tarray )
return [];
var r = new Array();
var i = 0;
var l = __dollar__asize(a);
while( i < l ) {
if( __dollar__typeof(a[i]) == __dollar__tstring )
r.push(new String(a[i]));
i += 1;
}
return r;
}
public static function getEnv( s : String ) : String {
var v = get_env(untyped s.__s);
if( v == null )
return null;
return new String(v);
}
public static function putEnv( s : String, v : String ) : Void {
untyped put_env(s.__s,if( v == null ) null else v.__s);
}
public static function sleep( seconds : Float ) : Void {
_sleep(seconds);
}
public static function setTimeLocale( loc : String ) : Bool {
return set_time_locale(untyped loc.__s);
}
public static function getCwd() : String {
return new String(get_cwd());
}
public static function setCwd( s : String ) : Void {
set_cwd(untyped s.__s);
}
public static function systemName() : String {
return new String(sys_string());
}
public static function command( cmd : String, ?args : Array<String> ) : Int {
if (args == null) {
return sys_command(untyped cmd.__s);
} else {
switch (systemName()) {
case "Windows":
cmd = [
for (a in [StringTools.replace(cmd, "/", "\\")].concat(args))
SysTools.quoteWinArg(a, true)
].join(" ");
return sys_command(untyped cmd.__s);
case _:
cmd = [cmd].concat(args).map(SysTools.quoteUnixArg).join(" ");
return sys_command(untyped cmd.__s);
}
}
}
public static function exit( code : Int ) : Void {
sys_exit(code);
}
public static function time() : Float {
return sys_time();
}
public static function cpuTime() : Float {
return sys_cpu_time();
}
@:deprecated("Use programPath instead") public static function executablePath() : String {
return new String(sys_exe_path());
}
public static function programPath() : String {
#if macro
return null;
#elseif interp
return new String(sys_program_path());
#else
return sys_program_path;
#end
}
public static function environment() : Map<String,String> {
var l : Array<Dynamic> = sys_env();
var h = new haxe.ds.StringMap();
while( l != null ) {
h.set(new String(l[0]),new String(l[1]));
l = l[2];
}
return h;
}
private static var get_env = neko.Lib.load("std","get_env",1);
private static var put_env = neko.Lib.load("std","put_env",2);
private static var _sleep = neko.Lib.load("std","sys_sleep",1);
private static var set_time_locale = neko.Lib.load("std","set_time_locale",1);
private static var get_cwd = neko.Lib.load("std","get_cwd",0);
private static var set_cwd = neko.Lib.load("std","set_cwd",1);
private static var sys_string = neko.Lib.load("std","sys_string",0);
private static var sys_command = neko.Lib.load("std","sys_command",1);
private static var sys_exit = neko.Lib.load("std","sys_exit",1);
private static var sys_time = neko.Lib.load("std","sys_time",0);
private static var sys_cpu_time = neko.Lib.load("std","sys_cpu_time",0);
private static var sys_exe_path = neko.Lib.load("std","sys_exe_path",0);
#if interp
private static var sys_program_path = neko.Lib.load("std","sys_program_path",0);
#elseif !macro
// It has to be initialized before any call to loadModule or Sys.setCwd()...
private static var sys_program_path = {
var m = neko.vm.Module.local().name;
if (m == "") { // it is likely neko embedded in an exe
var exe = new String(sys_exe_path());
try {
sys.FileSystem.fullPath(exe);
} catch (e:Dynamic) {
exe;
}
} else {
try {
sys.FileSystem.fullPath(m);
} catch (e:Dynamic) {
// maybe the neko module name was supplied without .n extension...
if (!StringTools.endsWith(m, ".n")) {
try {
sys.FileSystem.fullPath(m + ".n");
} catch (e:Dynamic) {
m;
}
} else {
m;
}
}
}
}
#end
private static var sys_env = neko.Lib.load("std","sys_env",0);
private static var file_stdin = neko.Lib.load("std","file_stdin",0);
private static var file_stdout = neko.Lib.load("std","file_stdout",0);
private static var file_stderr = neko.Lib.load("std","file_stderr",0);
private static var getch = neko.Lib.load("std","sys_getch",1);
}

View File

@ -0,0 +1,235 @@
/*
* 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 function getClass<T>( o : T ) : Class<T> untyped {
if( __dollar__typeof(o) != __dollar__tobject )
return null;
var p = __dollar__objgetproto(o);
if( p == null )
return null;
return p.__class__;
}
public static function getEnum( o : EnumValue ) : Enum<Dynamic> untyped {
if( __dollar__typeof(o) != __dollar__tobject )
return null;
return o.__enum__;
}
public static function getSuperClass( c : Class<Dynamic> ) : Class<Dynamic> untyped {
return c.__super__;
}
public static function getClassName( c : Class<Dynamic> ) : String {
if( c == null )
return null;
var a : Array<String> = untyped c.__name__;
return a.join(".");
}
public static function getEnumName( e : Enum<Dynamic> ) : String {
var a : Array<String> = untyped e.__ename__;
return a.join(".");
}
public static function resolveClass( name : String ) : Class<Dynamic> untyped {
var path = name.split(".");
var cl = Reflect.field(untyped neko.Boot.__classes,path[0]);
var i = 1;
while( cl != null && i < path.length ) {
cl = Reflect.field(cl,path[i]);
i += 1;
}
// ensure that this is a class
if( cl == null || cl.__name__ == null )
return null;
return cl;
}
public static function resolveEnum( name : String ) : Enum<Dynamic> untyped {
var path = name.split(".");
var e = Reflect.field(neko.Boot.__classes,path[0]);
var i = 1;
while( e != null && i < path.length ) {
e = Reflect.field(e,path[i]);
i += 1;
}
// ensure that this is an enum
if( e == null || e.__ename__ == null )
return null;
return e;
}
public static function createInstance<T>( cl : Class<T>, args : Array<Dynamic> ) : T untyped {
var fnew = $objget(cl,$hash("new".__s));
var a = args.__neko();
// pad missing args with null's
var n = $nargs(fnew);
if( n > $asize(a) ) {
var a2 = $amake(n);
$ablit(a2,0,a,0,$asize(a));
a = a2;
}
return $call(fnew,cl,a);
}
public static function createEmptyInstance<T>( cl : Class<T> ) : T untyped {
var o = __dollar__new(null);
__dollar__objsetproto(o,cl.prototype);
return o;
}
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 {
var c : String = (untyped e.__constructs__)[index];
if( c == null ) throw index+" is not a valid enum constructor index";
return createEnum(e,c,params);
}
public static function getInstanceFields( c : Class<Dynamic> ) : Array<String> {
var a = Reflect.fields(untyped c.prototype);
c = untyped c.__super__;
while( c != null ) {
for( f in Reflect.fields(untyped c.prototype) ) {
a.remove(f);
a.push(f);
}
c = untyped c.__super__;
}
a.remove("__class__");
a.remove("__serialize");
a.remove("__string");
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("__super__");
a.remove("__string");
a.remove("__construct__");
a.remove("__properties__");
a.remove("prototype");
a.remove("new");
#if (macro || interp)
a.remove("__ct__");
#end
return a;
}
public static function getEnumConstructs( e : Enum<Dynamic> ) : Array<String> {
var a : Array<String> = untyped e.__constructs__;
return a.copy();
}
public static function typeof( v : Dynamic ) : ValueType untyped {
return switch( __dollar__typeof(v) ) {
case 0: TNull;
case 1: TInt;
case 2: TFloat;
case 3: TBool;
case 7: TFunction;
case 5:
var c = v.__class__;
if( c != null )
TClass(c);
else {
var e = v.__enum__;
if( e != null )
TEnum(e);
else
TObject;
}
default: TUnknown;
}
}
public static function enumEq<T>( a : T, b : T ) : Bool untyped {
if( a == b )
return true;
try {
if( a.__enum__ == null || a.index != b.index )
return false;
} catch( e : Dynamic ) {
return false;
}
for( i in 0...__dollar__asize(a.args) )
if( !enumEq(a.args[i],b.args[i]) )
return false;
return true;
}
public static function enumConstructor( e : EnumValue ) : String {
return new String(untyped e.tag);
}
public static function enumParameters( e : EnumValue ) : Array<Dynamic> {
return untyped if( e.args == null ) [] else Array.new1(e.args,__dollar__asize(e.args));
}
public inline static function enumIndex( e : EnumValue ) : Int {
return untyped e.index;
}
public static function allEnums<T>( e : Enum<T> ) : Array<T> {
var all = [];
var cst : Array<String> = untyped e.__constructs__;
for( c in cst ) {
var v = Reflect.field(e,c);
if( !Reflect.isFunction(v) )
all.push(v);
}
return all;
}
}

View File

@ -0,0 +1,85 @@
package haxe;
@:coreApi
class Exception {
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;
@:noCompletion var __exceptionMessage:String;
@:noCompletion var __exceptionStack:Null<CallStack>;
@:noCompletion var __nativeStack:Any;
@:noCompletion @:ifFeature("haxe.Exception.get_stack") var __skipStack:Int = 0;
@:noCompletion var __nativeException:Any;
@:noCompletion var __previousException:Null<Exception>;
static function caught(value:Any):Exception {
if(Std.isOfType(value, Exception)) {
return value;
} else {
return new ValueException(value, null, value);
}
}
static function thrown(value:Any):Any {
if(Std.isOfType(value, Exception)) {
return (value:Exception).native;
} else {
var e = new ValueException(value);
e.__shiftStack();
return e;
}
}
public function new(message:String, ?previous:Exception, ?native:Any) {
__exceptionMessage = message;
__previousException = previous;
if(native != null) {
__nativeStack = NativeStackTrace.exceptionStack();
__nativeException = native;
} else {
__nativeStack = NativeStackTrace.callStack();
__shiftStack();
__nativeException = this;
}
}
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 __exceptionMessage;
}
function get_previous():Null<Exception> {
return __previousException;
}
final function get_native():Any {
return __nativeException;
}
function get_stack():CallStack {
return switch __exceptionStack {
case null:
__exceptionStack = NativeStackTrace.toHaxe(__nativeStack, __skipStack);
case s: s;
}
}
}

View File

@ -0,0 +1,51 @@
package haxe;
import haxe.CallStack.StackItem;
private typedef NativeTrace = {
final skip:Int;
final stack:Dynamic;
}
/**
Do not use manually.
**/
@:dox(hide)
@:noCompletion
class NativeStackTrace {
@:ifFeature('haxe.NativeStackTrace.exceptionStack')
static public inline function saveStack(exception:Any):Void {
}
static public inline function callStack():NativeTrace {
return { skip:1, stack:untyped __dollar__callstack() };
}
static public function exceptionStack():NativeTrace {
return { skip:0, stack:untyped __dollar__excstack() };
}
static public function toHaxe(native:NativeTrace, skip:Int = 0):Array<StackItem> {
skip += native.skip;
var a = new Array();
var l = untyped __dollar__asize(native.stack);
var i = 0;
while (i < l) {
var x = native.stack[l - i - 1];
//skip all CFunctions until we skip required amount of hx entries
if(x == null && skip > i) {
skip++;
}
if(skip > i++) {
continue;
}
if (x == null)
a.push(CFunction);
else if (untyped __dollar__typeof(x) == __dollar__tstring)
a.push(Module(new String(x)));
else
a.push(FilePos(null, new String(untyped x[0]), untyped x[1]));
}
return a;
}
}

View File

@ -0,0 +1,53 @@
/*
* 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
class Resource {
static var content:Array<{name:String, data:String, str:String}>;
public static function listNames():Array<String> {
return [for (x in content) x.name];
}
public static function getString(name:String):String {
for (x in content)
if (x.name == name) {
return new String(x.data);
}
return null;
}
public static function getBytes(name:String):haxe.io.Bytes {
for (x in content)
if (x.name == name) {
return haxe.io.Bytes.ofData(cast x.data);
}
return null;
}
static function __init__() : Void {
var tmp = untyped __resources__();
content = untyped Array.new1(tmp, __dollar__asize(tmp));
}
}

View File

@ -0,0 +1,107 @@
/*
* 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
@:deprecated('haxe.Utf8 is deprecated. Use UnicodeString instead.')
class Utf8 {
var __b:Dynamic;
public function new(?size:Int):Void {
__b = utf8_buf_alloc(if (size == null) 1 else size);
}
public function addChar(c:Int):Void {
utf8_buf_add(__b, c);
}
public function toString():String {
return new String(utf8_buf_content(__b));
}
public static function encode(s:String):String {
s = untyped s.__s;
var sl = untyped __dollar__ssize(s);
var buf:Dynamic = utf8_buf_alloc(sl);
var i = 0;
while (i < sl) {
utf8_buf_add(buf, untyped __dollar__sget(s, i));
i += 1;
}
return new String(utf8_buf_content(buf));
}
public static function decode(s:String):String {
s = untyped s.__s;
var sl = untyped __dollar__ssize(s);
var ret = untyped __dollar__smake(sl);
var i = 0;
utf8_iter(s, function(c) {
if (c == 8364) // euro symbol
c = 164;
else if (c == 0xFEFF) // BOM
return;
else if (c > 255)
throw "Utf8::decode invalid character (" + c + ")";
untyped __dollar__sset(ret, i, c);
i += 1;
});
return new String(untyped __dollar__ssub(ret, 0, i));
}
public static function iter(s:String, chars:Int->Void):Void {
utf8_iter(untyped s.__s, chars);
}
public static function charCodeAt(s:String, index:Int):Int {
return utf8_get(untyped s.__s, index);
}
public static function validate(s:String):Bool {
return utf8_validate(untyped s.__s);
}
public static function length(s:String):Int {
return utf8_length(untyped s.__s);
}
public static function compare(a:String, b:String):Int {
return utf8_compare(untyped a.__s, untyped b.__s);
}
public static function sub(s:String, pos:Int, len:Int):String {
return new String(utf8_sub(untyped s.__s, pos, len));
}
static var utf8_buf_alloc = neko.Lib.load("std", "utf8_buf_alloc", 1);
static var utf8_buf_add = neko.Lib.load("std", "utf8_buf_add", 2);
static var utf8_buf_content = neko.Lib.load("std", "utf8_buf_content", 1);
static var utf8_buf_length = neko.Lib.load("std", "utf8_buf_length", 1);
static var utf8_iter = neko.Lib.load("std", "utf8_iter", 2);
static var utf8_get = neko.Lib.load("std", "utf8_get", 2);
static var utf8_validate = neko.Lib.load("std", "utf8_validate", 1);
static var utf8_length = neko.Lib.load("std", "utf8_length", 1);
static var utf8_compare = neko.Lib.load("std", "utf8_compare", 2);
static var utf8_sub = neko.Lib.load("std", "utf8_sub", 3);
}

View File

@ -0,0 +1,36 @@
/*
* 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.crypto;
class Md5 {
public static function encode(s:String):String {
return untyped new String(base_encode(make_md5(s.__s), "0123456789abcdef".__s));
}
public static function make(b:haxe.io.Bytes):haxe.io.Bytes {
return haxe.io.Bytes.ofData(make_md5(b.getData()));
}
static var base_encode = neko.Lib.load("std", "base_encode", 2);
static var make_md5 = neko.Lib.load("std", "make_md5", 1);
}

View File

@ -0,0 +1,93 @@
/*
* 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 function new():Void {
h = untyped __dollar__hnew(0);
}
public inline function set(key:Int, value:T):Void {
untyped __dollar__hset(h, key, value, null);
}
public function get(key:Int):Null<T> {
return untyped __dollar__hget(h, key, null);
}
public inline function exists(key:Int):Bool {
return untyped __dollar__hmem(h, key, null);
}
public inline function remove(key:Int):Bool {
return untyped __dollar__hremove(h, key, null);
}
public function keys():Iterator<Int> {
var l = new List<Int>();
untyped __dollar__hiter(h, function(k, _) {
l.push(k);
});
return l.iterator();
}
public function iterator():Iterator<T> {
var l = new List<T>();
untyped __dollar__hiter(h, function(_, v) {
l.push(v);
});
return l.iterator();
}
@: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 = untyped __dollar__hnew(0);
}
}

View File

@ -0,0 +1,114 @@
/*
* 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;
@:coreApi
class ObjectMap<K:{}, V> implements haxe.Constraints.IMap<K, V> {
static var count = 0;
static inline function assignId(obj:{}):Int {
var newId = count++;
untyped obj.__id__ = newId;
return newId;
}
static inline function getId(obj:{}):Int {
return untyped obj.__id__;
}
var h:{};
var k:{};
public function new():Void {
h = untyped __dollar__hnew(0);
k = untyped __dollar__hnew(0);
}
public inline function set(key:K, value:V):Void
untyped {
var id = key.__id__ != null ? key.__id__ : assignId(key);
untyped __dollar__hset(h, id, value, null);
untyped __dollar__hset(k, id, key, null);
}
public function get(key:K):Null<V> {
return untyped __dollar__hget(h, getId(key), null);
}
public inline function exists(key:K):Bool {
return untyped __dollar__hmem(h, getId(key), null);
}
public function remove(key:K):Bool {
var id = getId(key);
untyped __dollar__hremove(h, id, null);
return untyped __dollar__hremove(k, id, null);
}
public function keys():Iterator<K> {
var l = new List<K>();
untyped __dollar__hiter(k, function(_, v) {
l.push(v);
});
return l.iterator();
}
public function iterator():Iterator<V> {
var l = new List<V>();
untyped __dollar__hiter(h, function(_, v) {
l.push(v);
});
return l.iterator();
}
@: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 = untyped __dollar__hnew(0);
k = untyped __dollar__hnew(0);
}
}

View File

@ -0,0 +1,93 @@
/*
* 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 StringMap<T> implements haxe.Constraints.IMap<String, T> {
private var h:Dynamic;
public function new():Void {
h = untyped __dollar__hnew(0);
}
public inline function set(key:String, value:T):Void {
untyped __dollar__hset(h, key.__s, value, null);
}
public inline function get(key:String):Null<T> {
return untyped __dollar__hget(h, key.__s, null);
}
public inline function exists(key:String):Bool {
return untyped __dollar__hmem(h, key.__s, null);
}
public inline function remove(key:String):Bool {
return untyped __dollar__hremove(h, key.__s, null);
}
public function keys():Iterator<String> {
var l = new List<String>();
untyped __dollar__hiter(h, function(k, _) {
l.push(new String(k));
});
return l.iterator();
}
public function iterator():Iterator<T> {
var l = new List<T>();
untyped __dollar__hiter(h, function(_, v) {
l.push(v);
});
return l.iterator();
}
@: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 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 = untyped __dollar__hnew(0);
}
}

View File

@ -0,0 +1,29 @@
/*
* 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;
class StringInput extends BytesInput {
public function new(s:String) {
super(neko.Lib.bytesReference(s));
}
}

View File

@ -0,0 +1,64 @@
/*
* 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.iterators;
class StringIteratorUnicode {
var byteOffset:Int = 0;
var s:String;
public inline function new(s:String) {
this.s = s;
}
public inline function hasNext() {
return byteOffset < s.length;
}
public inline function next() {
var code:Int = codeAt(byteOffset);
if (code < 0xC0) {
byteOffset++;
} else if (code < 0xE0) {
code = ((code - 0xC0) << 6) + codeAt(byteOffset + 1) - 0x80;
byteOffset += 2;
} else if (code < 0xF0) {
code = ((code - 0xE0) << 12) + ((codeAt(byteOffset + 1) - 0x80) << 6) + codeAt(byteOffset + 2) - 0x80;
byteOffset += 3;
} else {
code = ((code - 0xF0) << 18)
+ ((codeAt(byteOffset + 1) - 0x80) << 12)
+ ((codeAt(byteOffset + 2) - 0x80) << 6)
+ codeAt(byteOffset + 3)
- 0x80;
byteOffset += 4;
}
return code;
}
inline function codeAt(index:Int):Int {
return untyped $sget(s.__s, index);
}
static public inline function unicodeIterator(s:String) {
return new StringIteratorUnicode(s);
}
}

View File

@ -0,0 +1,65 @@
/*
* 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.iterators;
class StringKeyValueIteratorUnicode {
var byteOffset:Int = 0;
var charOffset:Int = 0;
var s:String;
public inline function new(s:String) {
this.s = s;
}
public inline function hasNext() {
return byteOffset < s.length;
}
public inline function next() {
var code:Int = codeAt(byteOffset);
if (code < 0xC0) {
byteOffset++;
} else if (code < 0xE0) {
code = ((code - 0xC0) << 6) + codeAt(byteOffset + 1) - 0x80;
byteOffset += 2;
} else if (code < 0xF0) {
code = ((code - 0xE0) << 12) + ((codeAt(byteOffset + 1) - 0x80) << 6) + codeAt(byteOffset + 2) - 0x80;
byteOffset += 3;
} else {
code = ((code - 0xF0) << 18)
+ ((codeAt(byteOffset + 1) - 0x80) << 12)
+ ((codeAt(byteOffset + 2) - 0x80) << 6)
+ codeAt(byteOffset + 3)
- 0x80;
byteOffset += 4;
}
return {key: charOffset++, value: code};
}
inline function codeAt(index:Int):Int {
return untyped $sget(s.__s, index);
}
static public inline function unicodeKeyValueIterator(s:String) {
return new StringKeyValueIteratorUnicode(s);
}
}

View File

@ -0,0 +1,61 @@
/*
* 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.zip;
@:coreApi
class Compress {
var s:Dynamic;
public function new(level:Int):Void {
s = _deflate_init(level);
}
public function execute(src:haxe.io.Bytes, srcPos:Int, dst:haxe.io.Bytes, dstPos:Int):{done:Bool, read:Int, write:Int} {
return _deflate_buffer(s, src.getData(), srcPos, dst.getData(), dstPos);
}
public function setFlushMode(f:FlushMode):Void {
_set_flush_mode(s, untyped Std.string(f).__s);
}
public function close():Void {
_deflate_end(s);
}
public static function run(s:haxe.io.Bytes, level:Int):haxe.io.Bytes {
var c = new Compress(level);
c.setFlushMode(FlushMode.FINISH);
var out = haxe.io.Bytes.alloc(_deflate_bound(c.s, s.length));
var r = c.execute(s, 0, out, 0);
c.close();
if (!r.done || r.read != s.length)
throw "Compression failed";
return out.sub(0, r.write);
}
static var _deflate_init = neko.Lib.load("zlib", "deflate_init", 1);
static var _deflate_bound = neko.Lib.load("zlib", "deflate_bound", 2);
static var _deflate_buffer = neko.Lib.load("zlib", "deflate_buffer", 5);
static var _deflate_end = neko.Lib.load("zlib", "deflate_end", 1);
static var _set_flush_mode = neko.Lib.load("zlib", "set_flush_mode", 2);
}

View File

@ -0,0 +1,68 @@
/*
* 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.zip;
@:coreApi
class Uncompress {
var s:Dynamic;
public function new(?windowBits:Int):Void {
s = _inflate_init(windowBits);
}
public function execute(src:haxe.io.Bytes, srcPos:Int, dst:haxe.io.Bytes, dstPos:Int):{done:Bool, read:Int, write:Int} {
return _inflate_buffer(s, src.getData(), srcPos, dst.getData(), dstPos);
}
public function setFlushMode(f:FlushMode):Void {
_set_flush_mode(s, untyped Std.string(f).__s);
}
public function close():Void {
_inflate_end(s);
}
public static function run(src:haxe.io.Bytes, ?bufsize:Int):haxe.io.Bytes {
var u = new Uncompress(null);
if (bufsize == null)
bufsize = 1 << 16; // 64K
var tmp = haxe.io.Bytes.alloc(bufsize);
var b = new haxe.io.BytesBuffer();
var pos = 0;
u.setFlushMode(FlushMode.SYNC);
while (true) {
var r = u.execute(src, pos, tmp, 0);
b.addBytes(tmp, 0, r.write);
pos += r.read;
if (r.done)
break;
}
u.close();
return b.getBytes();
}
static var _inflate_init = neko.Lib.load("zlib", "inflate_init", 1);
static var _inflate_buffer = neko.Lib.load("zlib", "inflate_buffer", 5);
static var _inflate_end = neko.Lib.load("zlib", "inflate_end", 1);
static var _set_flush_mode = neko.Lib.load("zlib", "set_flush_mode", 2);
}

View File

@ -0,0 +1,123 @@
/*
* 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 sys;
private enum FileKind {
kdir;
kfile;
kother(k:String);
}
@:coreApi
class FileSystem {
public static function exists(path:String):Bool {
return sys_exists(untyped (makeCompatiblePath(path)).__s);
}
public static function rename(path:String, newPath:String):Void {
untyped sys_rename(path.__s, newPath.__s);
}
public static function stat(path:String):FileStat {
var s:FileStat = sys_stat(untyped (makeCompatiblePath(path)).__s);
s.atime = untyped Date.new1(s.atime);
s.mtime = untyped Date.new1(s.mtime);
s.ctime = untyped Date.new1(s.ctime);
return s;
}
public static function fullPath(relPath:String):String {
return new String(file_full_path(untyped relPath.__s));
}
public static function absolutePath(relPath:String):String {
if (haxe.io.Path.isAbsolute(relPath))
return relPath;
return haxe.io.Path.join([Sys.getCwd(), relPath]);
}
static function kind(path:String):FileKind {
var k = new String(sys_file_type(untyped (makeCompatiblePath(path)).__s));
return switch (k) {
case "file": kfile;
case "dir": kdir;
default: kother(k);
}
}
public static inline function isDirectory(path:String):Bool {
return kind(path) == kdir;
}
public static function createDirectory(path:String):Void {
var path = haxe.io.Path.addTrailingSlash(path);
var _p = null;
var parts = [];
while (path != (_p = haxe.io.Path.directory(path))) {
parts.unshift(path);
path = _p;
}
for (part in parts) {
if (part.charCodeAt(part.length - 1) != ":".code && !exists(part))
sys_create_dir(untyped part.__s, 493);
}
}
public static function deleteFile(path:String):Void {
file_delete(untyped path.__s);
}
public static function deleteDirectory(path:String):Void {
sys_remove_dir(untyped path.__s);
}
public static function readDirectory(path:String):Array<String> {
var l:Array<Dynamic> = sys_read_dir(untyped path.__s);
var a = new Array();
while (l != null) {
a.push(new String(l[0]));
l = l[1];
}
return a;
}
private static inline function makeCompatiblePath(path:String):String {
return if (path.charCodeAt(1) == ":".code && path.length <= 3) {
haxe.io.Path.addTrailingSlash(path);
} else if (path == "/") {
"/";
} else {
haxe.io.Path.removeTrailingSlashes(path);
}
}
private static var sys_exists = neko.Lib.load("std", "sys_exists", 1);
private static var file_delete = neko.Lib.load("std", "file_delete", 1);
private static var sys_rename = neko.Lib.load("std", "sys_rename", 2);
private static var sys_stat = neko.Lib.load("std", "sys_stat", 1);
private static var sys_file_type = neko.Lib.load("std", "sys_file_type", 1);
private static var sys_create_dir = neko.Lib.load("std", "sys_create_dir", 2);
private static var sys_remove_dir = neko.Lib.load("std", "sys_remove_dir", 1);
private static var sys_read_dir = neko.Lib.load("std", "sys_read_dir", 1);
private static var file_full_path = neko.Lib.load("std", "file_full_path", 1);
}

View File

@ -0,0 +1,213 @@
/*
* 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 sys.db;
private class D {
static function load(fun, args):Dynamic {
return neko.Lib.load(lib, fun, args);
}
static var lib = try {
neko.Lib.load("mysql5", "connect", 1);
"mysql5";
} catch (e:Dynamic) "mysql";
public static var connect = load("connect", 1);
public static var select_db = load("select_db", 2);
public static var request = load("request", 2);
public static var close = load("close", 1);
public static var escape = load("escape", 2);
public static var set_conv_funs = load("set_conv_funs", 4);
public static var result_get_length = load("result_get_length", 1);
public static var result_get_nfields = load("result_get_nfields", 1);
public static var result_next = load("result_next", 1);
public static var result_get = load("result_get", 2);
public static var result_get_int = load("result_get_int", 2);
public static var result_get_float = load("result_get_float", 2);
public static var result_fields_names = neko.Lib.loadLazy(lib, "result_get_fields_names", 1);
}
private class MysqlResultSet implements sys.db.ResultSet {
public var length(get, null):Int;
public var nfields(get, null):Int;
private var __r:Dynamic;
private var cache:Dynamic;
public function new(r) {
__r = r;
}
private function get_length() {
return D.result_get_length(__r);
}
private function get_nfields() {
return D.result_get_nfields(__r);
}
public function hasNext() {
if (cache == null)
cache = next();
return (cache != null);
}
public function next():Dynamic {
var c = cache;
if (c != null) {
cache = null;
return c;
}
c = D.result_next(__r);
return c;
}
public function results():List<Dynamic> {
var l = new List();
while (hasNext())
l.add(next());
return l;
}
public function getResult(n:Int) {
return new String(D.result_get(__r, n));
}
public function getIntResult(n:Int):Int {
return D.result_get_int(__r, n);
}
public function getFloatResult(n:Int):Float {
return D.result_get_float(__r, n);
}
public function getFieldsNames():Array<String> {
var a = D.result_fields_names(__r);
untyped {
var i = 0;
var l = __dollar__asize(a);
while (i < l) {
a[i] = new String(a[i]);
i += 1;
}
a = Array.new1(cast a, l);
}
return a;
}
}
private class MysqlConnection implements sys.db.Connection {
private var __c:Dynamic;
public function new(c) {
__c = c;
D.set_conv_funs(c, function(s) return new String(s), function(d) return untyped Date.new1(d), function(b) return haxe.io.Bytes.ofData(b));
}
public function request(s:String):sys.db.ResultSet {
try {
var r = D.request(this.__c, untyped s.__s);
return new MysqlResultSet(r);
} catch (e:Dynamic) {
untyped if (__dollar__typeof(e) == __dollar__tobject && __dollar__typeof(e.msg) == __dollar__tstring)
e = e.msg;
untyped __dollar__rethrow(e);
return null;
}
}
public function close() {
D.close(__c);
}
public function escape(s:String) {
return new String(D.escape(__c, untyped s.__s));
}
public function quote(s:String) {
return "'" + escape(s) + "'";
}
public function addValue(s:StringBuf, v:Dynamic) {
var t = untyped __dollar__typeof(v);
if (untyped (t == __dollar__tint || t == __dollar__tnull))
s.add(v);
else if (untyped t == __dollar__tbool)
s.addChar(if (v) "1".code else "0".code);
else {
s.addChar("'".code);
s.add(escape(Std.string(v)));
s.addChar("'".code);
}
}
public function lastInsertId() {
return request("SELECT LAST_INSERT_ID()").getIntResult(0);
}
public function dbName() {
return "MySQL";
}
public function startTransaction() {
request("START TRANSACTION");
}
public function commit() {
request("COMMIT");
}
public function rollback() {
request("ROLLBACK");
}
private static var __use_date = Date;
}
@:coreApi class Mysql {
public static function connect(params:{
host:String,
?port:Int,
user:String,
pass:String,
?socket:String,
?database:String
}):sys.db.Connection {
var o = untyped {
host: params.host.__s,
port: if (params.port == null) 3306 else params.port,
user: params.user.__s,
pass: params.pass.__s,
socket: if (params.socket == null) null else params.socket.__s
};
var c = D.connect(o);
if (params.database != null) {
try {
D.select_db(c, untyped params.database.__s);
} catch (e:Dynamic) {
D.close(c);
neko.Lib.rethrow(e);
}
}
return new MysqlConnection(c);
}
}

View File

@ -0,0 +1,199 @@
/*
* 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 sys.db;
private class SqliteConnection implements Connection {
var c:Dynamic;
public function new(file:String) {
c = _connect(untyped file.__s);
}
public function close() {
_close(c);
}
public function request(s:String):ResultSet {
try {
return new SqliteResultSet(_request(c, untyped s.__s));
} catch (e:String) {
throw "Error while executing " + s + " (" + e + ")";
}
}
public function escape(s:String) {
return s.split("'").join("''");
}
public function quote(s:String) {
if (s.indexOf("\000") >= 0)
return "x'" + new String(untyped _encode(s.__s, "0123456789ABCDEF".__s)) + "'";
return "'" + s.split("'").join("''") + "'";
}
public function addValue(s:StringBuf, v:Dynamic) {
var t = untyped __dollar__typeof(v);
if (untyped (t == __dollar__tint || t == __dollar__tnull))
s.add(v);
else if (untyped t == __dollar__tbool)
s.add(if (v) 1 else 0);
else
s.add(quote(Std.string(v)));
}
public function lastInsertId() {
return _last_id(c);
}
public function dbName() {
return "SQLite";
}
public function startTransaction() {
request("BEGIN TRANSACTION");
}
public function commit() {
request("COMMIT");
}
public function rollback() {
request("ROLLBACK");
}
static var _encode = neko.Lib.load("std", "base_encode", 2);
static var _connect = neko.Lib.load("sqlite", "connect", 1);
static var _close = neko.Lib.load("sqlite", "close", 1);
static var _request = neko.Lib.load("sqlite", "request", 2);
static var _last_id = neko.Lib.load("sqlite", "last_insert_id", 1);
}
private class SqliteResultSet implements ResultSet {
public var length(get, null):Int;
public var nfields(get, null):Int;
var r:Dynamic;
var cache:List<Dynamic>;
public function new(r) {
cache = new List();
this.r = r;
hasNext(); // execute the request
}
function get_length() {
if (nfields != 0) {
while (true) {
var c = doNext();
if (c == null)
break;
cache.add(c);
}
return cache.length;
}
return result_get_length(r);
}
function get_nfields() {
return result_get_nfields(r);
}
public function hasNext() {
var c = next();
if (c == null)
return false;
cache.push(c);
return true;
}
public function next():Dynamic {
var c = cache.pop();
if (c != null)
return c;
return doNext();
}
private function doNext():Dynamic {
var c = result_next(r);
if (c == null)
return null;
untyped {
var f = __dollar__objfields(c);
var i = 0;
var l = __dollar__asize(f);
while (i < l) {
var v = __dollar__objget(c, f[i]);
if (__dollar__typeof(v) == __dollar__tstring)
__dollar__objset(c, f[i], new String(v));
i = i + 1;
}
}
return c;
}
public function results():List<Dynamic> {
var l = new List();
while (true) {
var c = next();
if (c == null)
break;
l.add(c);
}
return l;
}
public function getResult(n:Int) {
return new String(result_get(r, n));
}
public function getIntResult(n:Int):Int {
return result_get_int(r, n);
}
public function getFloatResult(n:Int):Float {
return result_get_float(r, n);
}
public function getFieldsNames():Array<String> {
if(hasNext()) {
return switch cache.first() {
case null: null;
case row: Reflect.fields(row);
}
}
return null;
}
static var result_next = neko.Lib.load("sqlite", "result_next", 1);
static var result_get_length = neko.Lib.load("sqlite", "result_get_length", 1);
static var result_get_nfields = neko.Lib.load("sqlite", "result_get_nfields", 1);
static var result_get = neko.Lib.load("sqlite", "result_get", 2);
static var result_get_int = neko.Lib.load("sqlite", "result_get_int", 2);
static var result_get_float = neko.Lib.load("sqlite", "result_get_float", 2);
}
@:coreApi class Sqlite {
public static function open(file:String):Connection {
return new SqliteConnection(file);
}
}

View File

@ -0,0 +1,77 @@
/*
* 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 sys.io;
enum FileHandle {}
@:coreApi class File {
public static function getContent(path:String):String {
return new String(file_contents(untyped path.__s));
}
public static function getBytes(path:String):haxe.io.Bytes {
return neko.Lib.bytesReference(getContent(path));
}
public static function saveContent(path:String, content:String):Void {
var f = write(path);
f.writeString(content);
f.close();
}
public static function saveBytes(path:String, bytes:haxe.io.Bytes):Void {
var f = write(path);
f.write(bytes);
f.close();
}
public static function read(path:String, binary:Bool = true):FileInput {
return untyped new FileInput(file_open(path.__s, (if (binary) "rb" else "r").__s));
}
public static function write(path:String, binary:Bool = true):FileOutput {
return untyped new FileOutput(file_open(path.__s, (if (binary) "wb" else "w").__s));
}
public static function append(path:String, binary:Bool = true):FileOutput {
return untyped new FileOutput(file_open(path.__s, (if (binary) "ab" else "a").__s));
}
public static function update(path:String, binary:Bool = true):FileOutput {
if (!FileSystem.exists(path)) {
write(path).close();
}
return untyped new FileOutput(file_open(path.__s, (if (binary) "rb+" else "r+").__s));
}
public static function copy(srcPath:String, dstPath:String):Void {
var s = read(srcPath, true);
var d = write(dstPath, true);
d.writeInput(s);
s.close();
d.close();
}
private static var file_contents = neko.Lib.load("std", "file_contents", 1);
private static var file_open = neko.Lib.load("std", "file_open", 2);
}

View File

@ -0,0 +1,83 @@
/*
* 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 sys.io;
@:coreApi class FileInput extends haxe.io.Input {
private var __f:File.FileHandle;
function new(f:File.FileHandle):Void {
__f = f;
}
public override function readByte():Int {
return try {
file_read_char(__f);
} catch (e:Dynamic) {
if (untyped __dollar__typeof(e) == __dollar__tarray)
throw new haxe.io.Eof();
else
throw haxe.io.Error.Custom(e);
}
}
public override function readBytes(s:haxe.io.Bytes, p:Int, l:Int):Int {
return try {
file_read(__f, s.getData(), p, l);
} catch (e:Dynamic) {
if (untyped __dollar__typeof(e) == __dollar__tarray)
throw new haxe.io.Eof();
else
throw haxe.io.Error.Custom(e);
}
}
public override function close():Void {
super.close();
file_close(__f);
}
public function seek(p:Int, pos:FileSeek):Void {
file_seek(__f, p, switch (pos) {
case SeekBegin: 0;
case SeekCur: 1;
case SeekEnd: 2;
});
}
public function tell():Int {
return file_tell(__f);
}
public function eof():Bool {
return file_eof(__f);
}
private static var file_eof = neko.Lib.load("std", "file_eof", 1);
private static var file_read = neko.Lib.load("std", "file_read", 4);
private static var file_read_char = neko.Lib.load("std", "file_read_char", 1);
private static var file_close = neko.Lib.load("std", "file_close", 1);
private static var file_seek = neko.Lib.load("std", "file_seek", 3);
private static var file_tell = neko.Lib.load("std", "file_tell", 1);
}

View File

@ -0,0 +1,71 @@
/*
* 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 sys.io;
@:coreApi class FileOutput extends haxe.io.Output {
private var __f:File.FileHandle;
function new(f:File.FileHandle):Void {
__f = f;
}
public override function writeByte(c:Int):Void {
try
file_write_char(__f, c)
catch (e:Dynamic)
throw haxe.io.Error.Custom(e);
}
public override function writeBytes(s:haxe.io.Bytes, p:Int, l:Int):Int {
return try file_write(__f, s.getData(), p, l) catch (e:Dynamic) throw haxe.io.Error.Custom(e);
}
public override function flush():Void {
file_flush(__f);
}
public override function close():Void {
super.close();
file_close(__f);
}
public function seek(p:Int, pos:FileSeek):Void {
file_seek(__f, p, switch (pos) {
case SeekBegin: 0;
case SeekCur: 1;
case SeekEnd: 2;
});
}
public function tell():Int {
return file_tell(__f);
}
private static var file_close = neko.Lib.load("std", "file_close", 1);
private static var file_seek = neko.Lib.load("std", "file_seek", 3);
private static var file_tell = neko.Lib.load("std", "file_tell", 1);
private static var file_flush = neko.Lib.load("std", "file_flush", 1);
private static var file_write = neko.Lib.load("std", "file_write", 4);
private static var file_write_char = neko.Lib.load("std", "file_write_char", 2);
}

View File

@ -0,0 +1,124 @@
/*
* 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 sys.io;
private class Stdin extends haxe.io.Output {
var p:Dynamic;
var buf:haxe.io.Bytes;
public function new(p:Dynamic) {
this.p = p;
buf = haxe.io.Bytes.alloc(1);
}
public override function close() {
super.close();
_stdin_close(p);
}
public override function writeByte(c) {
buf.set(0, c);
writeBytes(buf, 0, 1);
}
public override function writeBytes(buf:haxe.io.Bytes, pos:Int, len:Int):Int {
try {
return _stdin_write(p, buf.getData(), pos, len);
} catch (e:Dynamic) {
throw new haxe.io.Eof();
}
}
static var _stdin_write = neko.Lib.load("std", "process_stdin_write", 4);
static var _stdin_close = neko.Lib.load("std", "process_stdin_close", 1);
}
private class Stdout extends haxe.io.Input {
var p:Dynamic;
var out:Bool;
var buf:haxe.io.Bytes;
public function new(p:Dynamic, out) {
this.p = p;
this.out = out;
buf = haxe.io.Bytes.alloc(1);
}
public override function readByte() {
if (readBytes(buf, 0, 1) == 0)
throw haxe.io.Error.Blocked;
return buf.get(0);
}
public override function readBytes(str:haxe.io.Bytes, pos:Int, len:Int):Int {
try {
return (out ? _stdout_read : _stderr_read)(p, str.getData(), pos, len);
} catch (e:Dynamic) {
throw new haxe.io.Eof();
}
}
static var _stdout_read = neko.Lib.load("std", "process_stdout_read", 4);
static var _stderr_read = neko.Lib.load("std", "process_stderr_read", 4);
}
@:coreApi class Process {
var p:Dynamic;
public var stdout(default, null):haxe.io.Input;
public var stderr(default, null):haxe.io.Input;
public var stdin(default, null):haxe.io.Output;
public function new(cmd:String, ?args:Array<String>, ?detached:Bool):Void {
if (detached)
throw "Detached process is not supported on this platform";
p = try _run(untyped cmd.__s, neko.Lib.haxeToNeko(args)) catch (e:Dynamic) throw "Process creation failure : " + cmd;
stdin = new Stdin(p);
stdout = new Stdout(p, true);
stderr = new Stdout(p, false);
}
public function getPid():Int {
return _pid(p);
}
public function exitCode(block:Bool = true):Null<Int> {
if (block == false)
throw "Non blocking exitCode() not supported on this platform";
return _exit(p);
}
public function close():Void {
_close(p);
}
public function kill():Void {
_kill(p);
}
static var _run = neko.Lib.load("std", "process_run", 2);
static var _exit = neko.Lib.load("std", "process_exit", 1);
static var _pid = neko.Lib.load("std", "process_pid", 1);
static var _close = neko.Lib.loadLazy("std", "process_close", 1);
static var _kill = neko.Lib.loadLazy("std", "process_kill", 1);
}

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 sys.net;
@:coreApi
@:keepInit
class Host {
public var host(default, null):String;
public var ip(default, null):Int;
public function new(name:String):Void {
host = name;
ip = host_resolve(untyped name.__s);
}
public function toString():String {
return new String(host_to_string(ip));
}
public function reverse():String {
return new String(host_reverse(ip));
}
public static function localhost():String {
return new String(host_local());
}
static function __init__():Void {
neko.Lib.load("std", "socket_init", 0)();
}
private static var host_resolve = neko.Lib.load("std", "host_resolve", 1);
private static var host_reverse = neko.Lib.load("std", "host_reverse", 1);
private static var host_to_string = neko.Lib.load("std", "host_to_string", 1);
private static var host_local = neko.Lib.load("std", "host_local", 0);
}

View File

@ -0,0 +1,284 @@
/*
* 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 sys.net;
import haxe.io.Error;
@:callable
@:coreType
abstract SocketHandle {}
private class SocketOutput extends haxe.io.Output {
var __s:SocketHandle;
public function new(s) {
__s = s;
}
public override function writeByte(c:Int) {
try {
socket_send_char(__s, c);
} catch (e:Dynamic) {
if (e == "Blocking")
throw Blocked;
else if (e == "EOF")
throw new haxe.io.Eof();
else
throw Custom(e);
}
}
public override function writeBytes(buf:haxe.io.Bytes, pos:Int, len:Int):Int {
return try {
socket_send(__s, buf.getData(), pos, len);
} catch (e:Dynamic) {
if (e == "Blocking")
throw Blocked;
else
throw Custom(e);
}
}
public override function close() {
super.close();
if (__s != null)
socket_close(__s);
}
private static var socket_close = neko.Lib.load("std", "socket_close", 1);
private static var socket_send_char = neko.Lib.load("std", "socket_send_char", 2);
private static var socket_send = neko.Lib.load("std", "socket_send", 4);
}
private class SocketInput extends haxe.io.Input {
var __s:SocketHandle;
public function new(s) {
__s = s;
}
public override function readByte():Int {
return try {
socket_recv_char(__s);
} catch (e:Dynamic) {
if (e == "Blocking")
throw Blocked;
else if (__s == null)
throw Custom(e);
else
throw new haxe.io.Eof();
}
}
public override function readBytes(buf:haxe.io.Bytes, pos:Int, len:Int):Int {
var r;
try {
r = socket_recv(__s, buf.getData(), pos, len);
} catch (e:Dynamic) {
if (e == "Blocking")
throw Blocked;
else
throw Custom(e);
}
if (r == 0)
throw new haxe.io.Eof();
return r;
}
public override function close() {
super.close();
if (__s != null)
socket_close(__s);
}
private static var socket_recv = neko.Lib.load("std", "socket_recv", 4);
private static var socket_recv_char = neko.Lib.load("std", "socket_recv_char", 1);
private static var socket_close = neko.Lib.load("std", "socket_close", 1);
}
@:coreApi
class Socket {
private var __s:SocketHandle;
public var input(default, null):haxe.io.Input;
public var output(default, null):haxe.io.Output;
public var custom:Dynamic;
public function new():Void {
init();
}
private function init():Void {
if (__s == null)
__s = socket_new(false);
input = new SocketInput(__s);
output = new SocketOutput(__s);
}
public function close():Void {
socket_close(__s);
untyped {
input.__s = null;
output.__s = null;
}
input.close();
output.close();
}
public function read():String {
return new String(socket_read(__s));
}
public function write(content:String):Void {
socket_write(__s, untyped content.__s);
}
public function connect(host:Host, port:Int):Void {
try {
socket_connect(__s, host.ip, port);
} catch (s:String) {
if (s == "std@socket_connect")
throw "Failed to connect on " + host.toString() + ":" + port;
else if (s == "Blocking") {
// Do nothing, this is not a real error, it simply indicates
// that a non-blocking connect is in progress
} else
neko.Lib.rethrow(s);
}
}
public function listen(connections:Int):Void {
socket_listen(__s, connections);
}
public function shutdown(read:Bool, write:Bool):Void {
socket_shutdown(__s, read, write);
}
public function bind(host:Host, port:Int):Void {
socket_bind(__s, host.ip, port);
}
public function accept():Socket {
var c = socket_accept(__s);
var s = Type.createEmptyInstance(Socket);
s.__s = c;
s.input = new SocketInput(c);
s.output = new SocketOutput(c);
return s;
}
public function peer():{host:Host, port:Int} {
var a:Dynamic = socket_peer(__s);
if (a == null) {
return null;
}
var h = new Host("127.0.0.1");
untyped h.ip = a[0];
return {host: h, port: a[1]};
}
public function host():{host:Host, port:Int} {
var a:Dynamic = socket_host(__s);
if (a == null) {
return null;
}
var h = new Host("127.0.0.1");
untyped h.ip = a[0];
return {host: h, port: a[1]};
}
public function setTimeout(timeout:Float):Void {
socket_set_timeout(__s, timeout);
}
public function waitForRead():Void {
select([this], null, null, null);
}
public function setBlocking(b:Bool):Void {
socket_set_blocking(__s, b);
}
public function setFastSend(b:Bool):Void {
socket_set_fast_send(__s, b);
}
public static function select(read:Array<Socket>, write:Array<Socket>, others:Array<Socket>,
?timeout:Float):{read:Array<Socket>, write:Array<Socket>, others:Array<Socket>} {
var c = untyped __dollar__hnew(1);
var f = function(a:Array<Socket>) {
if (a == null)
return null;
untyped {
var r = __dollar__amake(a.length);
var i = 0;
while (i < a.length) {
r[i] = a[i].__s;
__dollar__hadd(c, a[i].__s, a[i]);
i += 1;
}
return r;
}
}
var neko_array = socket_select(f(read), f(write), f(others), timeout);
var g = function(a):Array<Socket> {
if (a == null)
return null;
var r = new Array();
var i = 0;
while (i < untyped __dollar__asize(a)) {
var t = untyped __dollar__hget(c, a[i], null);
if (t == null)
throw "Socket object not found.";
r[i] = t;
i += 1;
}
return r;
}
return {
read: g(neko_array[0]),
write: g(neko_array[1]),
others: g(neko_array[2])
};
}
private static var socket_new = neko.Lib.load("std", "socket_new", 1);
private static var socket_close = neko.Lib.load("std", "socket_close", 1);
private static var socket_write = neko.Lib.load("std", "socket_write", 2);
private static var socket_read = neko.Lib.load("std", "socket_read", 1);
private static var socket_connect = neko.Lib.load("std", "socket_connect", 3);
private static var socket_listen = neko.Lib.load("std", "socket_listen", 2);
private static var socket_select = neko.Lib.load("std", "socket_select", 4);
private static var socket_bind = neko.Lib.load("std", "socket_bind", 3);
private static var socket_accept = neko.Lib.load("std", "socket_accept", 1);
private static var socket_peer = neko.Lib.load("std", "socket_peer", 1);
private static var socket_host = neko.Lib.load("std", "socket_host", 1);
private static var socket_set_timeout = neko.Lib.load("std", "socket_set_timeout", 2);
private static var socket_shutdown = neko.Lib.load("std", "socket_shutdown", 3);
private static var socket_set_blocking = neko.Lib.load("std", "socket_set_blocking", 2);
private static var socket_set_fast_send = neko.Lib.loadLazy("std", "socket_set_fast_send", 2);
}

View File

@ -0,0 +1,67 @@
/*
* 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 sys.net;
import haxe.io.Error;
@:coreApi
class UdpSocket extends Socket {
private override function init():Void {
__s = Socket.socket_new(true);
super.init();
}
public function sendTo(buf:haxe.io.Bytes, pos:Int, len:Int, addr:Address):Int {
return try {
socket_send_to(__s, buf.getData(), pos, len, addr);
} catch (e:Dynamic) {
if (e == "Blocking")
throw Blocked;
else
throw Custom(e);
}
}
public function readFrom(buf:haxe.io.Bytes, pos:Int, len:Int, addr:Address):Int {
var r;
try {
r = socket_recv_from(__s, buf.getData(), pos, len, addr);
} catch (e:Dynamic) {
if (e == "Blocking")
throw Blocked;
else
throw Custom(e);
}
if (r == 0)
throw new haxe.io.Eof();
return r;
}
public function setBroadcast(b:Bool):Void {
socket_set_broadcast(__s, b);
}
static var socket_recv_from = neko.Lib.loadLazy("std", "socket_recv_from", 5);
static var socket_send_to = neko.Lib.loadLazy("std", "socket_send_to", 5);
static var socket_set_broadcast = neko.Lib.loadLazy("std", "socket_set_broadcast", 2);
}

View File

@ -0,0 +1,150 @@
/*
* 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 sys.ssl;
@:coreApi
class Certificate {
var __h:Null<Certificate>;
var __x:Dynamic;
@:allow(sys.ssl.Socket)
function new(x:Dynamic, ?h:Certificate) {
__x = x;
__h = h;
}
public static function loadFile(file:String):Certificate {
return new Certificate(cert_load_file(untyped file.__s));
}
public static function loadPath(path:String):Certificate {
return new Certificate(cert_load_path(untyped path.__s));
}
public static function fromString(str:String):Certificate {
return new Certificate(cert_add_pem(null, untyped str.__s));
}
public static function loadDefaults():Certificate {
var x = cert_load_defaults();
if (x != null)
return new Certificate(x);
var defPaths = null;
switch (Sys.systemName()) {
case "Linux":
defPaths = [
"/etc/ssl/certs/ca-certificates.crt", // Debian/Ubuntu/Gentoo etc.
"/etc/pki/tls/certs/ca-bundle.crt", // Fedora/RHEL
"/etc/ssl/ca-bundle.pem", // OpenSUSE
"/etc/pki/tls/cacert.pem", // OpenELEC
"/etc/ssl/certs", // SLES10/SLES11
"/system/etc/security/cacerts" // Android
];
case "BSD":
defPaths = [
"/usr/local/share/certs/ca-root-nss.crt", // FreeBSD/DragonFly
"/etc/ssl/cert.pem", // OpenBSD
"/etc/openssl/certs/ca-certificates.crt", // NetBSD
];
case "Android":
defPaths = ["/system/etc/security/cacerts"];
default:
}
if (defPaths != null) {
for (path in defPaths) {
if (sys.FileSystem.exists(path)) {
if (sys.FileSystem.isDirectory(path))
return loadPath(path);
else
return loadFile(path);
}
}
}
return null;
}
public var commonName(get, null):Null<String>;
public var altNames(get, null):Array<String>;
public var notBefore(get, null):Date;
public var notAfter(get, null):Date;
function get_commonName():Null<String> {
return subject("CN");
}
function get_altNames():Array<String> {
var l:Dynamic = cert_get_altnames(__x);
var a = new Array<String>();
while (l != null) {
a.push(new String(l[0]));
l = l[1];
}
return a;
}
public function subject(field:String):Null<String> {
var s = cert_get_subject(__x, untyped field.__s);
return s == null ? null : new String(cast s);
}
public function issuer(field:String):Null<String> {
var s = cert_get_issuer(__x, untyped field.__s);
return s == null ? null : new String(cast s);
}
function get_notBefore():Date {
var a = cert_get_notbefore(__x);
return new Date(a[0], a[1] - 1, a[2], a[3], a[4], a[5]);
}
function get_notAfter():Date {
var a = cert_get_notafter(__x);
return new Date(a[0], a[1] - 1, a[2], a[3], a[4], a[5]);
}
public function next():Null<Certificate> {
var n = cert_get_next(__x);
return n == null ? null : new Certificate(n, __h == null ? this : __h);
}
public function add(pem:String):Void {
cert_add_pem(__x, untyped pem.__s);
}
public function addDER(der:haxe.io.Bytes):Void {
cert_add_der(__x, der.getData());
}
private static var cert_load_defaults = neko.Lib.loadLazy("ssl", "cert_load_defaults", 0);
private static var cert_load_file = neko.Lib.loadLazy("ssl", "cert_load_file", 1);
private static var cert_load_path = neko.Lib.loadLazy("ssl", "cert_load_path", 1);
private static var cert_get_subject = neko.Lib.loadLazy("ssl", "cert_get_subject", 2);
private static var cert_get_issuer = neko.Lib.loadLazy("ssl", "cert_get_issuer", 2);
private static var cert_get_altnames = neko.Lib.loadLazy("ssl", "cert_get_altnames", 1);
private static var cert_get_notbefore = neko.Lib.loadLazy("ssl", "cert_get_notbefore", 1);
private static var cert_get_notafter = neko.Lib.loadLazy("ssl", "cert_get_notafter", 1);
private static var cert_get_next = neko.Lib.loadLazy("ssl", "cert_get_next", 1);
private static var cert_add_pem = neko.Lib.loadLazy("ssl", "cert_add_pem", 2);
private static var cert_add_der = neko.Lib.loadLazy("ssl", "cert_add_der", 2);
}

View File

@ -0,0 +1,42 @@
/*
* 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 sys.ssl;
@:coreApi
class Digest {
public static function make(data:haxe.io.Bytes, alg:DigestAlgorithm):haxe.io.Bytes {
return haxe.io.Bytes.ofData(dgst_make(data.getData(), untyped alg.__s));
}
public static function sign(data:haxe.io.Bytes, privKey:Key, alg:DigestAlgorithm):haxe.io.Bytes {
return haxe.io.Bytes.ofData(dgst_sign(data.getData(), @:privateAccess privKey.__k, untyped alg.__s));
}
public static function verify(data:haxe.io.Bytes, signature:haxe.io.Bytes, pubKey:Key, alg:DigestAlgorithm):Bool {
return dgst_verify(data.getData(), signature.getData(), @:privateAccess pubKey.__k, untyped alg.__s);
}
private static var dgst_make = neko.Lib.loadLazy("ssl", "dgst_make", 2);
private static var dgst_sign = neko.Lib.loadLazy("ssl", "dgst_sign", 3);
private static var dgst_verify = neko.Lib.loadLazy("ssl", "dgst_verify", 4);
}

View File

@ -0,0 +1,54 @@
/*
* 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 sys.ssl;
private typedef PKEY = Dynamic;
@:coreApi
class Key {
private var __k:PKEY;
private function new(k:PKEY) {
__k = k;
}
public static function loadFile(file:String, ?isPublic:Bool, ?pass:String):Key {
var data = sys.io.File.getBytes(file);
var str = neko.Lib.stringReference(data);
if (str.indexOf("-----BEGIN ") >= 0)
return readPEM(str, isPublic == true, pass);
else
return readDER(data, isPublic == true);
}
public static function readPEM(data:String, isPublic:Bool, ?pass:String):Key {
return new Key(key_from_pem(untyped data.__s, isPublic, pass == null ? null : untyped pass.__s));
}
public static function readDER(data:haxe.io.Bytes, isPublic:Bool):Key {
return new Key(key_from_der(data.getData(), isPublic));
}
private static var key_from_pem = neko.Lib.loadLazy("ssl", "key_from_pem", 3);
private static var key_from_der = neko.Lib.loadLazy("ssl", "key_from_der", 2);
}

View File

@ -0,0 +1,316 @@
/*
* 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 sys.ssl;
private typedef SocketHandle = Dynamic;
private typedef CTX = Dynamic;
private typedef SSL = Dynamic;
private class SocketInput extends haxe.io.Input {
@:allow(sys.ssl.Socket) private var __s:Socket;
public function new(s:Socket) {
this.__s = s;
}
public override function readByte() {
return try {
__s.handshake();
ssl_recv_char(@:privateAccess __s.ssl);
} catch (e:Dynamic) {
if (e == "Blocking")
throw haxe.io.Error.Blocked;
else if (__s == null)
throw haxe.io.Error.Custom(e);
else
throw new haxe.io.Eof();
}
}
public override function readBytes(buf:haxe.io.Bytes, pos:Int, len:Int):Int {
var r:Int;
if (__s == null)
throw "Invalid handle";
try {
__s.handshake();
r = ssl_recv(@:privateAccess __s.ssl, buf.getData(), pos, len);
} catch (e:Dynamic) {
if (e == "Blocking")
throw haxe.io.Error.Blocked;
else
throw haxe.io.Error.Custom(e);
}
if (r == 0)
throw new haxe.io.Eof();
return r;
}
public override function close() {
super.close();
if (__s != null)
__s.close();
}
private static var ssl_recv = neko.Lib.loadLazy("ssl", "ssl_recv", 4);
private static var ssl_recv_char = neko.Lib.loadLazy("ssl", "ssl_recv_char", 1);
}
private class SocketOutput extends haxe.io.Output {
@:allow(sys.ssl.Socket) private var __s:Socket;
public function new(s:Socket) {
this.__s = s;
}
public override function writeByte(c:Int) {
if (__s == null)
throw "Invalid handle";
try {
__s.handshake();
ssl_send_char(@:privateAccess __s.ssl, c);
} catch (e:Dynamic) {
if (e == "Blocking")
throw haxe.io.Error.Blocked;
else
throw haxe.io.Error.Custom(e);
}
}
public override function writeBytes(buf:haxe.io.Bytes, pos:Int, len:Int):Int {
return try {
__s.handshake();
ssl_send(@:privateAccess __s.ssl, buf.getData(), pos, len);
} catch (e:Dynamic) {
if (e == "Blocking")
throw haxe.io.Error.Blocked;
else
throw haxe.io.Error.Custom(e);
}
}
public override function close() {
super.close();
if (__s != null)
__s.close();
}
private static var ssl_send_char = neko.Lib.loadLazy("ssl", "ssl_send_char", 2);
private static var ssl_send = neko.Lib.loadLazy("ssl", "ssl_send", 4);
}
@:coreApi
class Socket extends sys.net.Socket {
public static var DEFAULT_VERIFY_CERT:Null<Bool> = true;
public static var DEFAULT_CA:Null<Certificate>;
private var ctx:CTX;
private var ssl:SSL;
public var verifyCert:Null<Bool>;
private var caCert:Null<Certificate>;
private var hostname:String;
private var ownCert:Null<Certificate>;
private var ownKey:Null<Key>;
private var altSNIContexts:Null<Array<{match:String->Bool, key:Key, cert:Certificate}>>;
private var sniCallback:Dynamic;
private var handshakeDone:Bool;
private override function init():Void {
__s = socket_new(false);
input = new SocketInput(this);
output = new SocketOutput(this);
if (DEFAULT_VERIFY_CERT && DEFAULT_CA == null) {
try {
DEFAULT_CA = Certificate.loadDefaults();
} catch (e:Dynamic) {}
}
verifyCert = DEFAULT_VERIFY_CERT;
caCert = DEFAULT_CA;
}
public override function connect(host:sys.net.Host, port:Int):Void {
try {
ctx = buildSSLContext(false);
ssl = ssl_new(ctx);
ssl_set_socket(ssl, __s);
handshakeDone = false;
if (hostname == null)
hostname = host.host;
if (hostname != null)
ssl_set_hostname(ssl, untyped hostname.__s);
socket_connect(__s, host.ip, port);
handshake();
} catch (s:String) {
if (s == "std@socket_connect")
throw "Failed to connect on " + host.host + ":" + port;
else
neko.Lib.rethrow(s);
} catch (e:Dynamic) {
neko.Lib.rethrow(e);
}
}
public function handshake():Void {
if (!handshakeDone) {
try {
ssl_handshake(ssl);
handshakeDone = true;
} catch (e:Dynamic) {
if (e == "Blocking")
throw haxe.io.Error.Blocked;
else
neko.Lib.rethrow(e);
}
}
}
public function setCA(cert:Certificate):Void {
caCert = cert;
}
public function setHostname(name:String):Void {
hostname = name;
}
public function setCertificate(cert:Certificate, key:Key):Void {
ownCert = cert;
ownKey = key;
}
public override function read():String {
handshake();
var b = ssl_read(ssl);
if (b == null)
return "";
return new String(cast b);
}
public override function write(content:String):Void {
handshake();
ssl_write(ssl, untyped content.__s);
}
public override function close():Void {
if (ssl != null)
ssl_close(ssl);
if (ctx != null)
conf_close(ctx);
if (altSNIContexts != null)
sniCallback = null;
socket_close(__s);
var input:SocketInput = cast input;
var output:SocketOutput = cast output;
@:privateAccess input.__s = output.__s = null;
input.close();
output.close();
}
public function addSNICertificate(cbServernameMatch:String->Bool, cert:Certificate, key:Key):Void {
if (altSNIContexts == null)
altSNIContexts = [];
altSNIContexts.push({match: cbServernameMatch, cert: cert, key: key});
}
public override function bind(host:sys.net.Host, port:Int):Void {
ctx = buildSSLContext(true);
socket_bind(__s, host.ip, port);
}
public override function accept():Socket {
var c = socket_accept(__s);
var ssl = ssl_new(ctx);
ssl_set_socket(ssl, c);
var s = Type.createEmptyInstance(sys.ssl.Socket);
s.__s = c;
s.ssl = ssl;
s.input = new SocketInput(s);
s.output = new SocketOutput(s);
s.handshakeDone = false;
return s;
}
public function peerCertificate():sys.ssl.Certificate {
var x = ssl_get_peer_certificate(ssl);
return x == null ? null : new sys.ssl.Certificate(x);
}
private function buildSSLContext(server:Bool):CTX {
var ctx:CTX = conf_new(server);
if (ownCert != null && ownKey != null)
conf_set_cert(ctx, @:privateAccess ownCert.__x, @:privateAccess ownKey.__k);
if (altSNIContexts != null) {
sniCallback = function(servername) {
var servername = new String(cast servername);
for (c in altSNIContexts) {
if (c.match(servername))
return @:privateAccess {
key:c.key.__k, cert:c.cert.__x
};
}
if (ownKey != null && ownCert != null)
return @:privateAccess {
key:ownKey.__k, cert:ownCert.__x
};
return null;
}
conf_set_servername_callback(ctx, sniCallback);
}
if (caCert != null)
conf_set_ca(ctx, caCert == null ? null : @:privateAccess caCert.__x);
conf_set_verify(ctx, verifyCert);
return ctx;
}
private static var ssl_new = neko.Lib.loadLazy("ssl", "ssl_new", 1);
private static var ssl_close = neko.Lib.loadLazy("ssl", "ssl_close", 1);
private static var ssl_handshake = neko.Lib.loadLazy("ssl", "ssl_handshake", 1);
private static var ssl_set_socket = neko.Lib.loadLazy("ssl", "ssl_set_socket", 2);
private static var ssl_set_hostname = neko.Lib.loadLazy("ssl", "ssl_set_hostname", 2);
private static var ssl_get_peer_certificate = neko.Lib.loadLazy("ssl", "ssl_get_peer_certificate", 1);
private static var ssl_read = neko.Lib.loadLazy("ssl", "ssl_read", 1);
private static var ssl_write = neko.Lib.loadLazy("ssl", "ssl_write", 2);
private static var conf_new = neko.Lib.loadLazy("ssl", "conf_new", 1);
private static var conf_close = neko.Lib.loadLazy("ssl", "conf_close", 1);
private static var conf_set_ca = neko.Lib.loadLazy("ssl", "conf_set_ca", 2);
private static var conf_set_verify = neko.Lib.loadLazy("ssl", "conf_set_verify", 2);
private static var conf_set_cert = neko.Lib.loadLazy("ssl", "conf_set_cert", 3);
private static var conf_set_servername_callback = neko.Lib.loadLazy("ssl", "conf_set_servername_callback", 2);
private static var socket_new = neko.Lib.load("std", "socket_new", 1);
private static var socket_close = neko.Lib.load("std", "socket_close", 1);
private static var socket_connect = neko.Lib.load("std", "socket_connect", 3);
private static var socket_bind = neko.Lib.load("std", "socket_bind", 3);
private static var socket_accept = neko.Lib.load("std", "socket_accept", 1);
}

View File

@ -0,0 +1,49 @@
/*
* 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 sys.thread;
@:coreApi
class Deque<T> {
var q:Dynamic;
public function new() {
q = deque_create();
}
public function add(i:T):Void {
deque_add(q, i);
}
public function push(i:T):Void {
deque_push(q, i);
}
public function pop(block:Bool):Null<T> {
return deque_pop(q, block);
}
static var deque_create = neko.Lib.loadLazy("std", "deque_create", 0);
static var deque_add = neko.Lib.loadLazy("std", "deque_add", 2);
static var deque_push = neko.Lib.loadLazy("std", "deque_push", 2);
static var deque_pop = neko.Lib.loadLazy("std", "deque_pop", 2);
}

View File

@ -0,0 +1,44 @@
/*
* 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 sys.thread;
@:coreApi
class Lock {
var l:Dynamic;
public function new() {
l = lock_create();
}
public function wait(?timeout:Float):Bool {
return lock_wait(l, timeout);
}
public function release():Void {
lock_release(l);
}
static var lock_create = neko.Lib.load("std", "lock_create", 0);
static var lock_release = neko.Lib.load("std", "lock_release", 1);
static var lock_wait = neko.Lib.load("std", "lock_wait", 2);
}

View File

@ -0,0 +1,49 @@
/*
* 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 sys.thread;
@:coreApi
class Mutex {
var m:Dynamic;
public function new():Void {
m = mutex_create();
}
public function acquire():Void {
mutex_acquire(m);
}
public function tryAcquire():Bool {
return mutex_try(m);
}
public function release():Void {
mutex_release(m);
}
static var mutex_create = neko.Lib.loadLazy("std", "mutex_create", 0);
static var mutex_release = neko.Lib.loadLazy("std", "mutex_release", 1);
static var mutex_acquire = neko.Lib.loadLazy("std", "mutex_acquire", 1);
static var mutex_try = neko.Lib.loadLazy("std", "mutex_try", 1);
}

View File

@ -0,0 +1,188 @@
/*
* 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 sys.thread;
private typedef ThreadImpl = HaxeThread;
abstract Thread(ThreadImpl) from ThreadImpl {
public var events(get,never):EventLoop;
public inline function sendMessage(msg:Dynamic):Void {
this.sendMessage(msg);
}
public static inline function current():Thread {
return HaxeThread.current();
}
public static inline function create(job:()->Void):Thread {
return HaxeThread.create(job, false);
}
public static inline function runWithEventLoop(job:()->Void):Void {
HaxeThread.runWithEventLoop(job);
}
public static inline function createWithEventLoop(job:()->Void):Thread {
return HaxeThread.create(job, true);
}
public static inline function readMessage(block:Bool):Dynamic {
return HaxeThread.readMessage(block);
}
function get_events():EventLoop {
if(this.events == null)
throw new NoEventLoopException();
return this.events;
}
@:keep
static function processEvents() {
HaxeThread.current().events.loop();
}
}
@:callable
@:coreType
private abstract NativeThreadHandle {}
private typedef ThreadHandle = NativeThreadHandle;
private class HaxeThread {
static var thread_create:(callb:(_:Dynamic)->Void, _:Dynamic)->ThreadHandle;
static var thread_current:()->ThreadHandle;
static var thread_send:(handle:ThreadHandle, msg:Dynamic)->Void;
static var thread_read_message:(block:Bool)->Dynamic;
static var mainThreadHandle:ThreadHandle;
static var mainThread:HaxeThread;
static var threads:Array<{thread:HaxeThread, handle:ThreadHandle}>;
static var threadsMutex:Mutex;
static function __init__() {
thread_create = neko.Lib.load("std", "thread_create", 2);
thread_current = neko.Lib.load("std", "thread_current", 0);
thread_send = neko.Lib.load("std", "thread_send", 2);
thread_read_message = neko.Lib.load("std", "thread_read_message", 1);
mainThreadHandle = thread_current();
mainThread = new HaxeThread(mainThreadHandle);
mainThread.events = new EventLoop();
threads = [];
threadsMutex = new Mutex();
}
public var events(default,null):Null<EventLoop>;
public var handle:ThreadHandle;
static public function current():HaxeThread {
var handle = thread_current();
if(handle == mainThreadHandle) {
return mainThread;
}
threadsMutex.acquire();
var thread = null;
for(item in threads) {
if(item.handle == handle) {
thread = item.thread;
break;
}
}
if(thread == null) {
thread = new HaxeThread(handle);
threads.push({thread:thread, handle:handle});
}
threadsMutex.release();
return thread;
}
public static function create(callb:()->Void, withEventLoop:Bool):Thread {
var item = {handle:null, thread:new HaxeThread(null)};
threadsMutex.acquire();
threads.push(item);
threadsMutex.release();
if(withEventLoop)
item.thread.events = new EventLoop();
item.handle = thread_create(_ -> {
if(item.thread.handle == null) {
item.handle = thread_current();
item.thread.handle = item.handle;
}
try {
callb();
if(withEventLoop)
item.thread.events.loop();
} catch(e) {
dropThread(item);
throw e;
}
dropThread(item);
}, null);
item.thread.handle = item.handle;
return item.thread;
}
public static function runWithEventLoop(job:()->Void):Void {
var thread = current();
if(thread.events == null) {
thread.events = new EventLoop();
try {
job();
thread.events.loop();
thread.events = null;
} catch(e) {
thread.events = null;
throw e;
}
} else {
job();
}
}
static function dropThread(deleteItem) {
threadsMutex.acquire();
for(i => item in threads) {
if(item == deleteItem) {
threads.splice(i, 1);
break;
}
}
threadsMutex.release();
}
public static inline function readMessage(block:Bool):Dynamic {
return thread_read_message(block);
}
public function new(handle:ThreadHandle) {
this.handle = handle;
}
public function sendMessage(msg:Dynamic) {
thread_send(handle, msg);
}
}

View File

@ -0,0 +1,47 @@
/*
* 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 sys.thread;
@:coreApi
class Tls<T> {
var t:Dynamic;
public var value(get, set):T;
public function new() {
t = tls_create();
}
function get_value():T {
return tls_get(t);
}
function set_value(v:T):T {
tls_set(t, v);
return v;
}
static var tls_create = neko.Lib.load("std", "tls_create", 0);
static var tls_get = neko.Lib.load("std", "tls_get", 1);
static var tls_set = neko.Lib.load("std", "tls_set", 2);
}