Add garbage collection to native pointer values

This commit is contained in:
Joshua Granick
2015-09-22 13:30:57 -07:00
parent b3d172f7ce
commit 0e42ea9159
19 changed files with 756 additions and 588 deletions

View File

@@ -46,11 +46,57 @@ DEFINE_KIND (k_finalizer);
namespace lime {
double lime_application_create (value callback) {
void gc_application (value handle) {
Application* app = CreateApplication ();
Application* application = (Application*)val_data (handle);
delete application;
}
void gc_font (value handle) {
#ifdef LIME_FREETYPE
Font *font = (Font*)val_data (handle);
delete font;
#endif
}
void gc_renderer (value handle) {
Renderer* renderer = (Renderer*)val_data (handle);
delete renderer;
}
void gc_text_layout (value handle) {
#ifdef LIME_HARFBUZZ
TextLayout *text = (TextLayout*)val_data (handle);
delete text;
#endif
}
void gc_window (value handle) {
Window* window = (Window*)val_data (handle);
delete window;
}
value lime_application_create (value callback) {
Application* application = CreateApplication ();
Application::callback = new AutoGCRoot (callback);
return (intptr_t)app;
value handle = cffi::alloc_pointer (application);
val_gc (handle, gc_application);
return handle;
}
@@ -63,41 +109,41 @@ namespace lime {
}
int lime_application_exec (double application) {
int lime_application_exec (value application) {
Application* app = (Application*)(intptr_t)application;
Application* app = (Application*)val_data (application);
return app->Exec ();
}
void lime_application_init (double application) {
void lime_application_init (value application) {
Application* app = (Application*)(intptr_t)application;
Application* app = (Application*)val_data (application);
app->Init ();
}
int lime_application_quit (double application) {
int lime_application_quit (value application) {
Application* app = (Application*)(intptr_t)application;
Application* app = (Application*)val_data (application);
return app->Quit ();
}
void lime_application_set_frame_rate (double application, double frameRate) {
void lime_application_set_frame_rate (value application, double frameRate) {
Application* app = (Application*)(intptr_t)application;
Application* app = (Application*)val_data (application);
app->SetFrameRate (frameRate);
}
bool lime_application_update (double application) {
bool lime_application_update (value application) {
Application* app = (Application*)(intptr_t)application;
Application* app = (Application*)val_data (application);
return app->Update ();
}
@@ -254,20 +300,10 @@ namespace lime {
}
void lime_font_destroy (value handle) {
int lime_font_get_ascender (value fontHandle) {
#ifdef LIME_FREETYPE
Font *font = (Font*)(intptr_t)val_float (handle);
delete font;
#endif
}
int lime_font_get_ascender (double fontHandle) {
#ifdef LIME_FREETYPE
Font *font = (Font*)(intptr_t)fontHandle;
Font *font = (Font*)val_data (fontHandle);
return font->GetAscender ();
#else
return 0;
@@ -276,10 +312,10 @@ namespace lime {
}
int lime_font_get_descender (double fontHandle) {
int lime_font_get_descender (value fontHandle) {
#ifdef LIME_FREETYPE
Font *font = (Font*)(intptr_t)fontHandle;
Font *font = (Font*)val_data (fontHandle);
return font->GetDescender ();
#else
return 0;
@@ -288,10 +324,10 @@ namespace lime {
}
value lime_font_get_family_name (double fontHandle) {
value lime_font_get_family_name (value fontHandle) {
#ifdef LIME_FREETYPE
Font *font = (Font*)(intptr_t)fontHandle;
Font *font = (Font*)val_data (fontHandle);
wchar_t *name = font->GetFamilyName ();
value result = alloc_wstring (name);
delete name;
@@ -303,10 +339,10 @@ namespace lime {
}
int lime_font_get_glyph_index (double fontHandle, HxString character) {
int lime_font_get_glyph_index (value fontHandle, HxString character) {
#ifdef LIME_FREETYPE
Font *font = (Font*)(intptr_t)fontHandle;
Font *font = (Font*)val_data (fontHandle);
return font->GetGlyphIndex ((char*)character.__s);
#else
return -1;
@@ -315,10 +351,10 @@ namespace lime {
}
value lime_font_get_glyph_indices (double fontHandle, HxString characters) {
value lime_font_get_glyph_indices (value fontHandle, HxString characters) {
#ifdef LIME_FREETYPE
Font *font = (Font*)(intptr_t)fontHandle;
Font *font = (Font*)val_data (fontHandle);
return font->GetGlyphIndices ((char*)characters.__s);
#else
return alloc_null ();
@@ -327,10 +363,10 @@ namespace lime {
}
value lime_font_get_glyph_metrics (double fontHandle, int index) {
value lime_font_get_glyph_metrics (value fontHandle, int index) {
#ifdef LIME_FREETYPE
Font *font = (Font*)(intptr_t)fontHandle;
Font *font = (Font*)val_data (fontHandle);
return font->GetGlyphMetrics (index);
#else
return alloc_null ();
@@ -339,10 +375,10 @@ namespace lime {
}
int lime_font_get_height (double fontHandle) {
int lime_font_get_height (value fontHandle) {
#ifdef LIME_FREETYPE
Font *font = (Font*)(intptr_t)fontHandle;
Font *font = (Font*)val_data (fontHandle);
return font->GetHeight ();
#else
return 0;
@@ -351,10 +387,10 @@ namespace lime {
}
int lime_font_get_num_glyphs (double fontHandle) {
int lime_font_get_num_glyphs (value fontHandle) {
#ifdef LIME_FREETYPE
Font *font = (Font*)(intptr_t)fontHandle;
Font *font = (Font*)val_data (fontHandle);
return font->GetNumGlyphs ();
#else
return alloc_null ();
@@ -363,10 +399,10 @@ namespace lime {
}
int lime_font_get_underline_position (double fontHandle) {
int lime_font_get_underline_position (value fontHandle) {
#ifdef LIME_FREETYPE
Font *font = (Font*)(intptr_t)fontHandle;
Font *font = (Font*)val_data (fontHandle);
return font->GetUnderlinePosition ();
#else
return 0;
@@ -375,10 +411,10 @@ namespace lime {
}
int lime_font_get_underline_thickness (double fontHandle) {
int lime_font_get_underline_thickness (value fontHandle) {
#ifdef LIME_FREETYPE
Font *font = (Font*)(intptr_t)fontHandle;
Font *font = (Font*)val_data (fontHandle);
return font->GetUnderlineThickness ();
#else
return 0;
@@ -387,10 +423,10 @@ namespace lime {
}
int lime_font_get_units_per_em (double fontHandle) {
int lime_font_get_units_per_em (value fontHandle) {
#ifdef LIME_FREETYPE
Font *font = (Font*)(intptr_t)fontHandle;
Font *font = (Font*)val_data (fontHandle);
return font->GetUnitsPerEM ();
#else
return 0;
@@ -421,9 +457,9 @@ namespace lime {
if (font->face) {
value v = alloc_float ((intptr_t)font);
val_gc (v, lime_font_destroy);
return v;
value handle = cffi::alloc_pointer (font);
val_gc (handle, gc_font);
return handle;
} else {
@@ -439,10 +475,10 @@ namespace lime {
}
value lime_font_outline_decompose (double fontHandle, int size) {
value lime_font_outline_decompose (value fontHandle, int size) {
#ifdef LIME_FREETYPE
Font *font = (Font*)(intptr_t)fontHandle;
Font *font = (Font*)val_data (fontHandle);
return font->Decompose (size);
#else
return alloc_null ();
@@ -451,10 +487,10 @@ namespace lime {
}
bool lime_font_render_glyph (double fontHandle, int index, value data) {
bool lime_font_render_glyph (value fontHandle, int index, value data) {
#ifdef LIME_FREETYPE
Font *font = (Font*)(intptr_t)fontHandle;
Font *font = (Font*)val_data (fontHandle);
Bytes bytes = Bytes (data);
return font->RenderGlyph (index, &bytes);
#else
@@ -464,10 +500,10 @@ namespace lime {
}
bool lime_font_render_glyphs (double fontHandle, value indices, value data) {
bool lime_font_render_glyphs (value fontHandle, value indices, value data) {
#ifdef LIME_FREETYPE
Font *font = (Font*)(intptr_t)fontHandle;
Font *font = (Font*)val_data (fontHandle);
Bytes bytes = Bytes (data);
return font->RenderGlyphs (indices, &bytes);
#else
@@ -477,10 +513,10 @@ namespace lime {
}
void lime_font_set_size (double fontHandle, int fontSize) {
void lime_font_set_size (value fontHandle, int fontSize) {
#ifdef LIME_FREETYPE
Font *font = (Font*)(intptr_t)fontHandle;
Font *font = (Font*)val_data (fontHandle);
font->SetSize (fontSize);
#endif
@@ -857,13 +893,13 @@ namespace lime {
}
void lime_mouse_warp (int x, int y, double window) {
void lime_mouse_warp (int x, int y, value window) {
Window* windowRef = 0;
if (window) {
windowRef = (Window*)(intptr_t)window;
windowRef = (Window*)val_data (window);
}
@@ -927,55 +963,57 @@ namespace lime {
}
double lime_renderer_create (double window) {
value lime_renderer_create (value window) {
Renderer* renderer = CreateRenderer ((Window*)(intptr_t)window);
return (intptr_t)renderer;
Renderer* renderer = CreateRenderer ((Window*)val_data (window));
value handle = cffi::alloc_pointer (renderer);
val_gc (handle, gc_renderer);
return handle;
}
void lime_renderer_flip (double renderer) {
void lime_renderer_flip (value renderer) {
((Renderer*)(intptr_t)renderer)->Flip ();
((Renderer*)val_data (renderer))->Flip ();
}
double lime_renderer_get_context (double renderer) {
double lime_renderer_get_context (value renderer) {
Renderer* targetRenderer = (Renderer*)(intptr_t)renderer;
Renderer* targetRenderer = (Renderer*)val_data (renderer);
return (intptr_t)targetRenderer->GetContext ();
}
value lime_renderer_get_type (double renderer) {
value lime_renderer_get_type (value renderer) {
Renderer* targetRenderer = (Renderer*)(intptr_t)renderer;
Renderer* targetRenderer = (Renderer*)val_data (renderer);
const char* type = targetRenderer->Type ();
return type ? alloc_string (type) : alloc_null ();
}
value lime_renderer_lock (double renderer) {
value lime_renderer_lock (value renderer) {
return ((Renderer*)(intptr_t)renderer)->Lock ();
return ((Renderer*)val_data (renderer))->Lock ();
}
void lime_renderer_make_current (double renderer) {
void lime_renderer_make_current (value renderer) {
((Renderer*)(intptr_t)renderer)->MakeCurrent ();
((Renderer*)val_data (renderer))->MakeCurrent ();
}
void lime_renderer_unlock (double renderer) {
void lime_renderer_unlock (value renderer) {
((Renderer*)(intptr_t)renderer)->Unlock ();
((Renderer*)val_data (renderer))->Unlock ();
}
@@ -1025,25 +1063,14 @@ namespace lime {
}
void lime_text_layout_destroy (value textHandle) {
#ifdef LIME_HARFBUZZ
TextLayout *text = (TextLayout*)(intptr_t)val_float (textHandle);
delete text;
text = 0;
#endif
}
value lime_text_layout_create (int direction, HxString script, HxString language) {
#if defined(LIME_FREETYPE) && defined(LIME_HARFBUZZ)
TextLayout *text = new TextLayout (direction, script.__s, language.__s);
value v = alloc_float ((intptr_t)text);
val_gc (v, lime_text_layout_destroy);
return v;
value handle = cffi::alloc_pointer (text);
val_gc (handle, gc_text_layout);
return handle;
#else
@@ -1054,12 +1081,12 @@ namespace lime {
}
value lime_text_layout_position (double textHandle, double fontHandle, int size, HxString textString, value data) {
value lime_text_layout_position (value textHandle, value fontHandle, int size, HxString textString, value data) {
#if defined(LIME_FREETYPE) && defined(LIME_HARFBUZZ)
TextLayout *text = (TextLayout*)(intptr_t)textHandle;
Font *font = (Font*)(intptr_t)fontHandle;
TextLayout *text = (TextLayout*)val_data (textHandle);
Font *font = (Font*)val_data (fontHandle);
Bytes bytes = Bytes (data);
text->Position (font, size, textString.__s, &bytes);
return bytes.Value ();
@@ -1071,30 +1098,30 @@ namespace lime {
}
void lime_text_layout_set_direction (double textHandle, int direction) {
void lime_text_layout_set_direction (value textHandle, int direction) {
#if defined(LIME_FREETYPE) && defined(LIME_HARFBUZZ)
TextLayout *text = (TextLayout*)(intptr_t)textHandle;
TextLayout *text = (TextLayout*)val_data (textHandle);
text->SetDirection (direction);
#endif
}
void lime_text_layout_set_language (double textHandle, HxString language) {
void lime_text_layout_set_language (value textHandle, HxString language) {
#if defined(LIME_FREETYPE) && defined(LIME_HARFBUZZ)
TextLayout *text = (TextLayout*)(intptr_t)textHandle;
TextLayout *text = (TextLayout*)val_data (textHandle);
text->SetLanguage (language.__s);
#endif
}
void lime_text_layout_set_script (double textHandle, HxString script) {
void lime_text_layout_set_script (value textHandle, HxString script) {
#if defined(LIME_FREETYPE) && defined(LIME_HARFBUZZ)
TextLayout *text = (TextLayout*)(intptr_t)textHandle;
TextLayout *text = (TextLayout*)val_data (textHandle);
text->SetScript (script.__s);
#endif
@@ -1109,26 +1136,28 @@ namespace lime {
}
void lime_window_alert (double window, HxString message, HxString title) {
void lime_window_alert (value window, HxString message, HxString title) {
Window* targetWindow = (Window*)(intptr_t)window;
Window* targetWindow = (Window*)val_data (window);
targetWindow->Alert (message.__s, title.__s);
}
void lime_window_close (double window) {
void lime_window_close (value window) {
Window* targetWindow = (Window*)(intptr_t)window;
Window* targetWindow = (Window*)val_data (window);
targetWindow->Close ();
}
double lime_window_create (double application, int width, int height, int flags, HxString title) {
value lime_window_create (value application, int width, int height, int flags, HxString title) {
Window* window = CreateWindow ((Application*)(intptr_t)application, width, height, flags, title.__s);
return (intptr_t)window;
Window* window = CreateWindow ((Application*)val_data (application), width, height, flags, title.__s);
value handle = cffi::alloc_pointer (window);
val_gc (handle, gc_window);
return handle;
}
@@ -1141,114 +1170,114 @@ namespace lime {
}
void lime_window_focus (double window) {
void lime_window_focus (value window) {
Window* targetWindow = (Window*)(intptr_t)window;
Window* targetWindow = (Window*)val_data (window);
targetWindow->Focus ();
}
bool lime_window_get_enable_text_events (double window) {
bool lime_window_get_enable_text_events (value window) {
Window* targetWindow = (Window*)(intptr_t)window;
Window* targetWindow = (Window*)val_data (window);
return targetWindow->GetEnableTextEvents ();
}
int lime_window_get_height (double window) {
int lime_window_get_height (value window) {
Window* targetWindow = (Window*)(intptr_t)window;
Window* targetWindow = (Window*)val_data (window);
return targetWindow->GetHeight ();
}
int32_t lime_window_get_id (double window) {
int32_t lime_window_get_id (value window) {
Window* targetWindow = (Window*)(intptr_t)window;
Window* targetWindow = (Window*)val_data (window);
return (int32_t)targetWindow->GetID ();
}
int lime_window_get_width (double window) {
int lime_window_get_width (value window) {
Window* targetWindow = (Window*)(intptr_t)window;
Window* targetWindow = (Window*)val_data (window);
return targetWindow->GetWidth ();
}
int lime_window_get_x (double window) {
int lime_window_get_x (value window) {
Window* targetWindow = (Window*)(intptr_t)window;
Window* targetWindow = (Window*)val_data (window);
return targetWindow->GetX ();
}
int lime_window_get_y (double window) {
int lime_window_get_y (value window) {
Window* targetWindow = (Window*)(intptr_t)window;
Window* targetWindow = (Window*)val_data (window);
return targetWindow->GetY ();
}
void lime_window_move (double window, int x, int y) {
void lime_window_move (value window, int x, int y) {
Window* targetWindow = (Window*)(intptr_t)window;
Window* targetWindow = (Window*)val_data (window);
targetWindow->Move (x, y);
}
void lime_window_resize (double window, int width, int height) {
void lime_window_resize (value window, int width, int height) {
Window* targetWindow = (Window*)(intptr_t)window;
Window* targetWindow = (Window*)val_data (window);
targetWindow->Resize (width, height);
}
void lime_window_set_enable_text_events (double window, bool enabled) {
void lime_window_set_enable_text_events (value window, bool enabled) {
Window* targetWindow = (Window*)(intptr_t)window;
Window* targetWindow = (Window*)val_data (window);
targetWindow->SetEnableTextEvents (enabled);
}
bool lime_window_set_fullscreen (double window, bool fullscreen) {
bool lime_window_set_fullscreen (value window, bool fullscreen) {
Window* targetWindow = (Window*)(intptr_t)window;
Window* targetWindow = (Window*)val_data (window);
return targetWindow->SetFullscreen (fullscreen);
}
void lime_window_set_icon (double window, value buffer) {
void lime_window_set_icon (value window, value buffer) {
Window* targetWindow = (Window*)(intptr_t)window;
Window* targetWindow = (Window*)val_data (window);
ImageBuffer imageBuffer = ImageBuffer (buffer);
targetWindow->SetIcon (&imageBuffer);
}
bool lime_window_set_minimized (double window, bool fullscreen) {
bool lime_window_set_minimized (value window, bool fullscreen) {
Window* targetWindow = (Window*)(intptr_t)window;
Window* targetWindow = (Window*)val_data (window);
return targetWindow->SetMinimized (fullscreen);
}
value lime_window_set_title (double window, HxString title) {
value lime_window_set_title (value window, HxString title) {
Window* targetWindow = (Window*)(intptr_t)window;
Window* targetWindow = (Window*)val_data (window);
const char* result = targetWindow->SetTitle (title.__s);
return result ? alloc_string (result) : alloc_null ();

View File

@@ -876,62 +876,62 @@ namespace lime {
}
bool lime_alc_close_device (double device) {
bool lime_alc_close_device (value device) {
ALCdevice* alcDevice = (ALCdevice*)(intptr_t)device;
ALCdevice* alcDevice = (ALCdevice*)val_data (device);
return alcCloseDevice (alcDevice);
}
double lime_alc_create_context (double device, value attrlist) {
value lime_alc_create_context (value device, value attrlist) {
ALCdevice* alcDevice = (ALCdevice*)(intptr_t)device;
ALCdevice* alcDevice = (ALCdevice*)val_data (device);
int* list = val_array_int (attrlist);
ALCcontext* alcContext = alcCreateContext (alcDevice, list);
return (intptr_t)alcContext;
return cffi::alloc_pointer (alcContext);
}
void lime_alc_destroy_context (double context) {
void lime_alc_destroy_context (value context) {
ALCcontext* alcContext = (ALCcontext*)(intptr_t)context;
ALCcontext* alcContext = (ALCcontext*)val_data (context);
alcDestroyContext (alcContext);
}
double lime_alc_get_contexts_device (double context) {
value lime_alc_get_contexts_device (value context) {
ALCcontext* alcContext = (ALCcontext*)(intptr_t)context;
ALCcontext* alcContext = (ALCcontext*)val_data (context);
ALCdevice* alcDevice = alcGetContextsDevice (alcContext);
return (intptr_t)alcDevice;
return cffi::alloc_pointer (alcDevice);
}
double lime_alc_get_current_context () {
value lime_alc_get_current_context () {
ALCcontext* alcContext = alcGetCurrentContext ();
return (intptr_t)alcContext;
return cffi::alloc_pointer (alcContext);
}
int lime_alc_get_error (double device) {
int lime_alc_get_error (value device) {
ALCdevice* alcDevice = (ALCdevice*)(intptr_t)device;
ALCdevice* alcDevice = (ALCdevice*)val_data (device);
return alcGetError (alcDevice);
}
value lime_alc_get_integerv (double device, int param, int size) {
value lime_alc_get_integerv (value device, int param, int size) {
ALCdevice* alcDevice = (ALCdevice*)(intptr_t)device;
ALCdevice* alcDevice = (ALCdevice*)val_data (device);
ALCint* values = new ALCint[size];
alcGetIntegerv (alcDevice, param, size, values);
@@ -950,43 +950,43 @@ namespace lime {
}
value lime_alc_get_string (double device, int param) {
value lime_alc_get_string (value device, int param) {
ALCdevice* alcDevice = (ALCdevice*)(intptr_t)device;
ALCdevice* alcDevice = (ALCdevice*)val_data (device);
const char* result = alcGetString (alcDevice, param);
return result ? alloc_string (result) : alloc_null ();
}
bool lime_alc_make_context_current (double context) {
bool lime_alc_make_context_current (value context) {
ALCcontext* alcContext = (ALCcontext*)(intptr_t)context;
ALCcontext* alcContext = (ALCcontext*)val_data (context);
return alcMakeContextCurrent (alcContext);
}
double lime_alc_open_device (HxString devicename) {
value lime_alc_open_device (HxString devicename) {
ALCdevice* alcDevice = alcOpenDevice (devicename.__s);
atexit (lime_al_cleanup);
return (intptr_t)alcDevice;
return cffi::alloc_pointer (alcDevice);
}
void lime_alc_process_context (double context) {
void lime_alc_process_context (value context) {
ALCcontext* alcContext = (ALCcontext*)(intptr_t)context;
ALCcontext* alcContext = (ALCcontext*)val_data (context);
alcProcessContext (alcContext);
}
void lime_alc_suspend_context (double context) {
void lime_alc_suspend_context (value context) {
ALCcontext* alcContext = (ALCcontext*)(intptr_t)context;
ALCcontext* alcContext = (ALCcontext*)val_data (context);
alcSuspendContext (alcContext);
}

File diff suppressed because it is too large Load Diff