/
Lib.hx
163 lines (140 loc) · 5.6 KB
/
Lib.hx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*
* 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 java;
/**
Platform-specific Java Library. Provides some platform-specific functions for the Java target,
such as conversion from Haxe types to native types and vice-versa.
**/
// we cannot use the java package for custom classes, so we're redefining it as "haxe.java.Lib"
@:native('haxe.java.Lib') class Lib {
/**
Print the specified value on the default output.
**/
inline public static function print(v:Dynamic):Void {
Sys.print(v);
}
/**
Print the specified value on the default output followed by a newline character.
**/
inline public static function println(v:Dynamic):Void {
Sys.println(v);
}
/**
Returns a native array from the supplied Array. This native array is unsafe to be written on,
as it may or may not be linked to the actual Array implementation.
If `equalLengthRequired` is true, the result might be a copy of an array with the correct size.
**/
inline public static function nativeArray<T>(arr:Array<T>, equalLengthRequired:Bool):NativeArray<T> {
var ret = new NativeArray(arr.length);
for (i in 0...arr.length) {
ret[i] = arr[i];
}
return ret;
}
/**
Gets the native `java.lang.Class` from the supplied object. Will throw an exception in case of null being passed.
[deprecated] - use `getNativeType` instead
**/
@:deprecated('The function `nativeType` is deprecated and will be removed in later versions. Please use `getNativeType` instead')
inline public static function nativeType<T>(obj:T):java.lang.Class<T> {
return untyped obj.getClass();
}
/**
Gets the native `java.lang.Class` from the supplied object. Will throw an exception in case of null being passed.
**/
inline public static function getNativeType<T>(obj:T):java.lang.Class<T> {
return untyped obj.getClass();
}
/**
Returns a Class<> equivalent to the native java.lang.Class type.
**/
public static inline function fromNativeType<T>(t:java.lang.Class<T>):Class<T> {
return untyped t;
}
/**
Returns a java.lang.Class equivalent to the Haxe Class<> type.
**/
public static inline function toNativeType<T>(cl:Class<T>):java.lang.Class<T> {
return untyped cl;
}
/**
Returns a java.lang.Class equivalent to the Haxe Enum<> type.
**/
public static inline function toNativeEnum<T>(cl:Enum<T>):java.lang.Class<T> {
return untyped cl;
}
/**
Returns a Haxe Array of a native Array.
Unless `copy` is true, it won't copy the contents of the native array,
so unless any operation triggers an array resize, all changes made to the Haxe array will affect the native array argument.
**/
@:generic public static function array<T>(native:java.NativeArray<T>):Array<T> {
return untyped Array.ofNative(native);
}
extern inline private static function doArray<T>(native:java.NativeArray<T>):Array<T> {
var ret:NativeArray<Dynamic> = new NativeArray(native.length);
for (i in 0...native.length) {
ret[i] = native[i];
}
return untyped Array.ofNative(ret);
}
public static function array_Int(native:java.NativeArray<Int>):Array<Int> {
return doArray(native);
}
public static function array_Float(native:java.NativeArray<Float>):Array<Float> {
return doArray(native);
}
public static function array_Bool(native:java.NativeArray<Bool>):Array<Bool> {
return doArray(native);
}
public static function array_java_Int8(native:java.NativeArray<java.StdTypes.Int8>):Array<java.StdTypes.Int8> {
return doArray(native);
}
public static function array_java_Int16(native:java.NativeArray<java.StdTypes.Int16>):Array<java.StdTypes.Int16> {
return doArray(native);
}
public static function array_java_Char16(native:java.NativeArray<java.StdTypes.Char16>):Array<java.StdTypes.Char16> {
return doArray(native);
}
public static function array_Single(native:java.NativeArray<Single>):Array<Single> {
return doArray(native);
}
public static function array_haxe_Int64(native:java.NativeArray<haxe.Int64>):Array<haxe.Int64> {
return doArray(native);
}
/**
Allocates a new Haxe Array with a predetermined size
**/
public static function arrayAlloc<T>(size:Int):Array<T> {
return untyped Array.alloc(size);
}
/**
Ensures that one thread does not enter a critical section of code while another thread
is in the critical section. If another thread attempts to enter a locked code, it
will wait, block, until the object is released.
This is the equivalent to "synchronized" in java code.
This method only exists at compile-time, so it can't be called via reflection.
**/
extern public static inline function lock<T>(obj:Dynamic, block:T):Void {
untyped __lock__(obj, block);
}
}