Work on node.js support

This commit is contained in:
vroad
2014-11-01 11:11:57 +09:00
parent 1fdc37a0b5
commit 17cb9e51b4
8 changed files with 188 additions and 52 deletions

View File

@@ -136,11 +136,28 @@ class Application extends Module {
*/ */
public function exec ():Int { public function exec ():Int {
#if (cpp || neko || nodejs) #if nodejs
var result = lime_application_exec (__handle); lime_application_init(__handle);
var eventLoop = function() {
var active = lime_application_update(__handle);
if (!active) {
var result = lime_application_quit(__handle);
__cleanup();
Sys.exit(result);
}
untyped setImmediate(eventLoop);
}
untyped setImmediate(eventLoop);
AudioManager.shutdown (); #elseif (cpp || neko)
lime_application_init(__handle);
while(lime_application_update(__handle))
{}
var result = lime_application_quit (__handle);
__cleanup();
return result; return result;
@@ -185,6 +202,13 @@ class Application extends Module {
} }
#if (cpp || neko || nodejs)
@:noCompletion private function __cleanup():Void {
AudioManager.shutdown();
}
#end
/** /**
* The init() method is called once before the first render() * The init() method is called once before the first render()
@@ -380,7 +404,9 @@ class Application extends Module {
#if (cpp || neko || nodejs) #if (cpp || neko || nodejs)
private static var lime_application_create = System.load ("lime", "lime_application_create", 1); private static var lime_application_create = System.load ("lime", "lime_application_create", 1);
private static var lime_application_exec = System.load ("lime", "lime_application_exec", 1); private static var lime_application_init = System.load ("lime", "lime_application_init", 1);
private static var lime_application_update = System.load ("lime", "lime_application_update", 1);
private static var lime_application_quit = System.load ("lime", "lime_application_quit", 1);
private static var lime_application_get_ticks = System.load ("lime", "lime_application_get_ticks", 0); private static var lime_application_get_ticks = System.load ("lime", "lime_application_get_ticks", 0);
private static var lime_update_event_manager_register = System.load ("lime", "lime_update_event_manager_register", 2); private static var lime_update_event_manager_register = System.load ("lime", "lime_update_event_manager_register", 2);
#end #end

View File

@@ -7,7 +7,7 @@ import js.html.HtmlElement;
import js.Browser; import js.Browser;
#end #end
#if sys #if (sys && !html5)
import sys.io.Process; import sys.io.Process;
#end #end
@@ -83,7 +83,7 @@ class System {
static private function findHaxeLib (library:String):String { static private function findHaxeLib (library:String):String {
#if sys #if (sys && !html5)
try { try {
@@ -149,7 +149,7 @@ class System {
} }
#if !disable_cffi #if !disable_cffi
#if sys #if (sys && !html5)
#if (iphone || emscripten || android || static_link) #if (iphone || emscripten || android || static_link)
return cpp.Lib.load (library, method, args); return cpp.Lib.load (library, method, args);
@@ -162,6 +162,8 @@ class System {
return cpp.Lib.load (__moduleNames.get (library), method, args); return cpp.Lib.load (__moduleNames.get (library), method, args);
#elseif neko #elseif neko
return neko.Lib.load (__moduleNames.get (library), method, args); return neko.Lib.load (__moduleNames.get (library), method, args);
#elseif nodejs
return js.Lib.load (__moduleNames.get (library), method, args);
#else #else
return null; return null;
#end #end
@@ -239,7 +241,7 @@ class System {
private static function sysName ():String { private static function sysName ():String {
#if sys #if (sys && !html5)
#if cpp #if cpp
var sys_string = cpp.Lib.load ("std", "sys_string", 0); var sys_string = cpp.Lib.load ("std", "sys_string", 0);
return sys_string (); return sys_string ();
@@ -255,7 +257,7 @@ class System {
private static function tryLoad (name:String, library:String, func:String, args:Int):Dynamic { private static function tryLoad (name:String, library:String, func:String, args:Int):Dynamic {
#if sys #if (sys && !html5)
try { try {
@@ -263,6 +265,8 @@ class System {
var result = cpp.Lib.load (name, func, args); var result = cpp.Lib.load (name, func, args);
#elseif (neko) #elseif (neko)
var result = neko.Lib.load (name, func, args); var result = neko.Lib.load (name, func, args);
#elseif nodejs
var result = js.Lib.load (name, func, args);
#else #else
var result = null; var result = null;
#end #end
@@ -290,7 +294,7 @@ class System {
private static function loaderTrace (message:String) { private static function loaderTrace (message:String) {
#if sys #if (sys && !html5)
#if cpp #if cpp
var get_env = cpp.Lib.load ("std", "get_env", 1); var get_env = cpp.Lib.load ("std", "get_env", 1);

View File

@@ -91,8 +91,13 @@ class ByteArray #if !html5 extends Bytes #end implements ArrayAccess<Int> #if !h
allocated = size < 16 ? 16 : size; allocated = size < 16 ? 16 : size;
var bytes = untyped __dollar__smake (allocated); var bytes = untyped __dollar__smake (allocated);
super (size, bytes); super (size, bytes);
#else
#if nodejs
var data = new BytesData(size);
data.fill(0);
#else #else
var data = new BytesData (); var data = new BytesData ();
#end
#if cpp #if cpp
NativeArray.setSize (data, size); NativeArray.setSize (data, size);
#else #else
@@ -209,6 +214,17 @@ class ByteArray #if !html5 extends Bytes #end implements ArrayAccess<Int> #if !h
b = new_b; b = new_b;
} }
#elseif nodejs
if (b == null)
b = new BytesData(len);
else
{
var new_b = new BytesData(len);
b.copy(new_b);
if (new_b.length > b.length)
new_b.fill(0, b.length, new_b.length);
b = new_b;
}
#else #else
if (b == null) if (b == null)
b = new BytesData (); b = new BytesData ();
@@ -382,6 +398,10 @@ class ByteArray #if !html5 extends Bytes #end implements ArrayAccess<Int> #if !h
var int = data.getInt32 (this.position, littleEndian); var int = data.getInt32 (this.position, littleEndian);
this.position += 4; this.position += 4;
return int; return int;
#elseif nodejs
var int = littleEndian ? b.readInt32LE(this.position) : b.readInt32BE(this.position);
this.position += 4;
return int;
#else #else
var ch1 = readUnsignedByte (); var ch1 = readUnsignedByte ();
var ch2 = readUnsignedByte (); var ch2 = readUnsignedByte ();
@@ -406,6 +426,10 @@ class ByteArray #if !html5 extends Bytes #end implements ArrayAccess<Int> #if !h
var short = data.getInt16 (this.position, littleEndian); var short = data.getInt16 (this.position, littleEndian);
this.position += 2; this.position += 2;
return short; return short;
#elseif nodejs
var short = littleEndian ? b.readInt16LE(this.position) : b.readInt16BE(this.position);
this.position += 2;
return short;
#else #else
var ch1 = readUnsignedByte (); var ch1 = readUnsignedByte ();
var ch2 = readUnsignedByte (); var ch2 = readUnsignedByte ();
@@ -434,6 +458,10 @@ class ByteArray #if !html5 extends Bytes #end implements ArrayAccess<Int> #if !h
var uInt = data.getUint32 (this.position, littleEndian); var uInt = data.getUint32 (this.position, littleEndian);
this.position += 4; this.position += 4;
return uInt; return uInt;
#elseif nodejs
var uInt = littleEndian ? b.readUInt32LE(this.position) : b.readUInt32BE(this.position);
this.position += 4;
return uInt;
#else #else
var ch1 = readUnsignedByte (); var ch1 = readUnsignedByte ();
var ch2 = readUnsignedByte (); var ch2 = readUnsignedByte ();
@@ -451,6 +479,10 @@ class ByteArray #if !html5 extends Bytes #end implements ArrayAccess<Int> #if !h
var uShort = data.getUint16 (this.position, littleEndian); var uShort = data.getUint16 (this.position, littleEndian);
this.position += 2; this.position += 2;
return uShort; return uShort;
#elseif nodejs
var uShort = littleEndian ? b.readInt16LE(this.position) : b.readInt16BE(this.position);
this.position += 2;
return uShort;
#else #else
var ch1 = readUnsignedByte (); var ch1 = readUnsignedByte ();
var ch2 = readUnsignedByte (); var ch2 = readUnsignedByte ();
@@ -524,6 +556,8 @@ class ByteArray #if !html5 extends Bytes #end implements ArrayAccess<Int> #if !h
var result = ""; var result = "";
untyped __global__.__hxcpp_string_of_bytes (b, result, p, len); untyped __global__.__hxcpp_string_of_bytes (b, result, p, len);
return result; return result;
#elseif nodejs
return untyped b.toString();
#else #else
return "-"; return "-";
#end #end
@@ -663,6 +697,8 @@ class ByteArray #if !html5 extends Bytes #end implements ArrayAccess<Int> #if !h
#if cpp #if cpp
untyped b.__unsafe_set (position++, byte); untyped b.__unsafe_set (position++, byte);
#elseif nodejs
b.writeUInt8 (byte, position++/*, true*/);
#else #else
untyped __dollar__sset (b, position++, byte & 0xff); untyped __dollar__sset (b, position++, byte & 0xff);
#end #end
@@ -688,6 +724,8 @@ class ByteArray #if !html5 extends Bytes #end implements ArrayAccess<Int> #if !h
ensureElem (position, true); ensureElem (position, true);
#if cpp #if cpp
b[position++] = untyped value; b[position++] = untyped value;
#elseif nodejs
b.writeUInt8(value, position++);
#else #else
untyped __dollar__sset (b, position++, value & 0xff); untyped __dollar__sset (b, position++, value & 0xff);
#end #end
@@ -767,7 +805,13 @@ class ByteArray #if !html5 extends Bytes #end implements ArrayAccess<Int> #if !h
this.position += 4; this.position += 4;
#else #else
ensureElem (position + 3, true); ensureElem (position + 3, true);
#if nodejs
if (littleEndian)
b.writeInt32LE(value, position);
else
b.writeInt32BE(value, position);
this.position += 4;
#else
if (littleEndian) { if (littleEndian) {
write_uncheck (value); write_uncheck (value);
@@ -784,6 +828,7 @@ class ByteArray #if !html5 extends Bytes #end implements ArrayAccess<Int> #if !h
} }
#end #end
#end
} }
@@ -796,7 +841,13 @@ class ByteArray #if !html5 extends Bytes #end implements ArrayAccess<Int> #if !h
this.position += 2; this.position += 2;
#else #else
ensureElem (position + 1, true); ensureElem (position + 1, true);
#if nodejs
if (littleEndian)
b.writeInt16LE(value, position);
else
b.writeInt16BE(value, position);
this.position += 2;
#else
if (littleEndian) { if (littleEndian) {
write_uncheck (value); write_uncheck (value);
@@ -808,7 +859,7 @@ class ByteArray #if !html5 extends Bytes #end implements ArrayAccess<Int> #if !h
write_uncheck (value); write_uncheck (value);
} }
#end
#end #end
} }
@@ -820,6 +871,13 @@ class ByteArray #if !html5 extends Bytes #end implements ArrayAccess<Int> #if !h
ensureWrite (this.position + 4); ensureWrite (this.position + 4);
data.setUint32 (this.position, value, littleEndian); data.setUint32 (this.position, value, littleEndian);
this.position += 4; this.position += 4;
#elseif nodejs
ensureElem (position + 3, true);
if (littleEndian)
b.writeUInt32LE(value, this.position);
else
b.writeUInt32BE(value, this.position);
this.position += 4;
#else #else
writeInt (value); writeInt (value);
#end #end
@@ -833,6 +891,12 @@ class ByteArray #if !html5 extends Bytes #end implements ArrayAccess<Int> #if !h
ensureWrite (this.position + 2); ensureWrite (this.position + 2);
data.setUint16 (this.position, value, littleEndian); data.setUint16 (this.position, value, littleEndian);
this.position += 2; this.position += 2;
#elseif nodejs
if (littleEndian)
b.writeUInt16LE(value, position);
else
b.writeUInt16BE(value, position);
this.position += 2;
#else #else
writeShort (value); writeShort (value);
#end #end
@@ -929,6 +993,8 @@ class ByteArray #if !html5 extends Bytes #end implements ArrayAccess<Int> #if !h
// No bounds checking is done in the cpp case // No bounds checking is done in the cpp case
#if cpp #if cpp
return untyped b[pos]; return untyped b[pos];
#elseif nodejs
return untyped b[pos];
#else #else
return get (pos); return get (pos);
#end #end

View File

@@ -17,7 +17,10 @@ namespace lime {
static AutoGCRoot* callback; static AutoGCRoot* callback;
virtual void Init() = 0;
virtual int Exec () = 0; virtual int Exec () = 0;
virtual bool Update() = 0;
virtual int Quit() = 0;
}; };

View File

@@ -43,6 +43,14 @@ namespace lime {
} }
value lime_application_init (value application) {
Application* app = (Application*)(intptr_t)val_float (application);
app->Init();
return alloc_null ();
}
value lime_application_exec (value application) { value lime_application_exec (value application) {
Application* app = (Application*)(intptr_t)val_float (application); Application* app = (Application*)(intptr_t)val_float (application);
@@ -50,6 +58,19 @@ namespace lime {
} }
value lime_application_update (value application) {
Application* app = (Application*)(intptr_t)val_float (application);
return alloc_bool (app->Update ());
}
value lime_application_quit (value application) {
Application* app = (Application*)(intptr_t)val_float (application);
return alloc_bool (app->Quit ());
}
value lime_application_get_ticks (value application) { value lime_application_get_ticks (value application) {
@@ -385,7 +406,10 @@ namespace lime {
DEFINE_PRIM (lime_application_create, 1); DEFINE_PRIM (lime_application_create, 1);
DEFINE_PRIM (lime_application_init, 1);
DEFINE_PRIM (lime_application_exec, 1); DEFINE_PRIM (lime_application_exec, 1);
DEFINE_PRIM (lime_application_update, 1);
DEFINE_PRIM (lime_application_quit, 1);
DEFINE_PRIM (lime_application_get_ticks, 0); DEFINE_PRIM (lime_application_get_ticks, 0);
DEFINE_PRIM (lime_audio_load, 1); DEFINE_PRIM (lime_audio_load, 1);
DEFINE_PRIM (lime_font_create_image, 1); DEFINE_PRIM (lime_font_create_image, 1);

View File

@@ -58,7 +58,7 @@ namespace lime {
static SDL_TimerID timerID = 0; static SDL_TimerID timerID = 0;
bool timerActive = false; bool timerActive = false;
bool firstTime = true;
Uint32 OnTimer (Uint32 interval, void *) { Uint32 OnTimer (Uint32 interval, void *) {
@@ -80,34 +80,43 @@ namespace lime {
} }
void SDLApplication::Init() {
int SDLApplication::Exec () {
framePeriod = 1000.0 / 60.0; framePeriod = 1000.0 / 60.0;
SDL_Event event;
active = true; active = true;
lastUpdate = SDL_GetTicks (); lastUpdate = SDL_GetTicks ();
nextUpdate = lastUpdate; nextUpdate = lastUpdate;
bool firstTime = true; }
while (active) { int SDLApplication::Exec () {
Init();
while (active)
Update();
return Quit();
}
bool SDLApplication::Update() {
SDL_Event event;
event.type = -1; event.type = -1;
while (active && (firstTime || SDL_WaitEvent (&event))) { if (active && (firstTime || SDL_WaitEvent (&event))) {
firstTime = false; firstTime = false;
HandleEvent (&event); HandleEvent (&event);
event.type = -1; event.type = -1;
if (!active) break; if (!active)
return active;
while (active && SDL_PollEvent (&event)) { if (SDL_PollEvent (&event)) {
HandleEvent (&event); HandleEvent (&event);
event.type = -1; event.type = -1;
if (!active) break;
} }
@@ -126,9 +135,11 @@ namespace lime {
} }
} }
return active;
} }
int SDLApplication::Quit() {
windowEvent.type = WINDOW_DEACTIVATE; windowEvent.type = WINDOW_DEACTIVATE;
WindowEvent::Dispatch (&windowEvent); WindowEvent::Dispatch (&windowEvent);
@@ -138,7 +149,6 @@ namespace lime {
} }
void SDLApplication::HandleEvent (SDL_Event* event) { void SDLApplication::HandleEvent (SDL_Event* event) {
switch (event->type) { switch (event->type) {

View File

@@ -22,7 +22,10 @@ namespace lime {
SDLApplication (); SDLApplication ();
~SDLApplication (); ~SDLApplication ();
virtual void Init();
virtual int Exec (); virtual int Exec ();
virtual bool Update();
virtual int Quit();
private: private:

View File

@@ -76,7 +76,7 @@ class ApplicationMain {
var result = app.exec (); var result = app.exec ();
#if sys #if (sys && !nodejs)
Sys.exit (result); Sys.exit (result);
#end #end