Skip to content

Commit

Permalink
Consistent return handling of HL CFFI string values
Browse files Browse the repository at this point in the history
  • Loading branch information
Joshua Granick committed Jan 4, 2024
1 parent 810e73d commit 88c2f6d
Show file tree
Hide file tree
Showing 17 changed files with 66 additions and 192 deletions.
7 changes: 4 additions & 3 deletions src/lime/_internal/backend/native/NativeApplication.hx
Expand Up @@ -8,6 +8,7 @@ import lime.graphics.OpenGLRenderContext;
import lime.graphics.RenderContext;
import lime.math.Rectangle;
import lime.media.AudioManager;
import lime.system.CFFI;
import lime.system.Clipboard;
import lime.system.Display;
import lime.system.DisplayMode;
Expand Down Expand Up @@ -187,7 +188,7 @@ class NativeApplication
{
for (window in parent.windows)
{
window.onDropFile.dispatch(#if hl @:privateAccess String.fromUTF8(dropEventInfo.file) #else dropEventInfo.file #end);
window.onDropFile.dispatch(CFFI.stringValue(dropEventInfo.file));
}
}

Expand Down Expand Up @@ -426,10 +427,10 @@ class NativeApplication
switch (textEventInfo.type)
{
case TEXT_INPUT:
window.onTextInput.dispatch(#if hl @:privateAccess String.fromUTF8(textEventInfo.text) #else textEventInfo.text #end);
window.onTextInput.dispatch(CFFI.stringValue(textEventInfo.text));

case TEXT_EDIT:
window.onTextEdit.dispatch(#if hl @:privateAccess String.fromUTF8(textEventInfo.text) #else textEventInfo.text #end, textEventInfo.start,
window.onTextEdit.dispatch(CFFI.stringValue(textEventInfo.text), textEventInfo.start,
textEventInfo.length);

default:
Expand Down
37 changes: 10 additions & 27 deletions src/lime/_internal/backend/native/NativeOpenGLRenderContext.hx
Expand Up @@ -20,6 +20,7 @@ import lime.graphics.opengl.GLTexture;
import lime.graphics.opengl.GLUniformLocation;
import lime.graphics.opengl.GL;
import lime.graphics.RenderContextType;
import lime.system.CFFI;
import lime.utils.DataPointer;
import lime.utils.Float32Array;
import lime.utils.Int32Array;
Expand Down Expand Up @@ -1395,7 +1396,7 @@ class NativeOpenGLRenderContext
return {
size: result.size,
type: result.type,
name: @:privateAccess String.fromUTF8(result.name)
name: CFFI.stringValue(result.name)
};
}
else
Expand All @@ -1420,7 +1421,7 @@ class NativeOpenGLRenderContext
return {
size: result.size,
type: result.type,
name: @:privateAccess String.fromUTF8(result.name)
name: CFFI.stringValue(result.name)
};
}
else
Expand Down Expand Up @@ -1455,10 +1456,7 @@ class NativeOpenGLRenderContext
{
#if (lime_cffi && (lime_opengl || lime_opengles) && !macro)
var result = NativeCFFI.lime_gl_get_active_uniform_block_name(__getObjectID(program), uniformBlockIndex);
#if hl
var result = @:privateAccess String.fromUTF8(result);
#end
return result;
return CFFI.stringValue(result);
#else
return null;
#end
Expand Down Expand Up @@ -1901,10 +1899,7 @@ class NativeOpenGLRenderContext
{
#if (lime_cffi && (lime_opengl || lime_opengles) && !macro)
var result = NativeCFFI.lime_gl_get_program_info_log(__getObjectID(program));
#if hl
var result = @:privateAccess String.fromUTF8(result);
#end
return result;
return CFFI.stringValue(result);
#else
return null;
#end
Expand Down Expand Up @@ -2039,10 +2034,7 @@ class NativeOpenGLRenderContext
{
#if (lime_cffi && (lime_opengl || lime_opengles) && !macro)
var result = NativeCFFI.lime_gl_get_shader_info_log(__getObjectID(shader));
#if hl
var result = (result != null) ? @:privateAccess String.fromUTF8(result) : null;
#end
return result;
return CFFI.stringValue(result);
#else
return null;
#end
Expand All @@ -2066,10 +2058,7 @@ class NativeOpenGLRenderContext
{
#if (lime_cffi && (lime_opengl || lime_opengles) && !macro)
var result = NativeCFFI.lime_gl_get_shader_source(__getObjectID(shader));
#if hl
var result = @:privateAccess String.fromUTF8(result);
#end
return result;
return CFFI.stringValue(result);
#else
return null;
#end
Expand All @@ -2079,10 +2068,7 @@ class NativeOpenGLRenderContext
{
#if (lime_cffi && (lime_opengl || lime_opengles) && !macro)
var result = NativeCFFI.lime_gl_get_string(name);
#if hl
var result = @:privateAccess String.fromUTF8(result);
#end
return result;
return CFFI.stringValue(result);
#else
return null;
#end
Expand All @@ -2092,10 +2078,7 @@ class NativeOpenGLRenderContext
{
#if (lime_cffi && (lime_opengl || lime_opengles) && !macro)
var result = NativeCFFI.lime_gl_get_stringi(name, index);
#if hl
var result = @:privateAccess String.fromUTF8(result);
#end
return result;
return CFFI.stringValue(result);
#else
return null;
#end
Expand Down Expand Up @@ -2207,7 +2190,7 @@ class NativeOpenGLRenderContext
return {
size: result.size,
type: result.type,
name: @:privateAccess String.fromUTF8(result.name)
name: CFFI.stringValue(result.name)
};
}
else
Expand Down
7 changes: 2 additions & 5 deletions src/lime/_internal/backend/native/NativeWindow.hx
Expand Up @@ -15,6 +15,7 @@ import lime.graphics.OpenGLRenderContext;
import lime.graphics.RenderContext;
import lime.math.Rectangle;
import lime.math.Vector2;
import lime.system.CFFI;
import lime.system.Display;
import lime.system.DisplayMode;
import lime.system.JNI;
Expand Down Expand Up @@ -123,11 +124,7 @@ class NativeWindow
var context = new RenderContext();
context.window = parent;

#if hl
var contextType = @:privateAccess String.fromUTF8(NativeCFFI.lime_window_get_context_type(handle));
#else
var contextType:String = NativeCFFI.lime_window_get_context_type(handle);
#end
var contextType:String = CFFI.stringValue(NativeCFFI.lime_window_get_context_type(handle));

switch (contextType)
{
Expand Down
6 changes: 1 addition & 5 deletions src/lime/graphics/cairo/Cairo.hx
Expand Up @@ -762,11 +762,7 @@ class Cairo
private static function get_versionString():String
{
#if (lime_cffi && lime_cairo && !macro)
#if hl
return @:privateAccess String.fromUTF8(NativeCFFI.lime_cairo_version_string());
#else
return NativeCFFI.lime_cairo_version_string();
#end
return CFFI.stringValue(NativeCFFI.lime_cairo_version_string());
#else
return "";
#end
Expand Down
6 changes: 2 additions & 4 deletions src/lime/media/openal/AL.hx
Expand Up @@ -2,6 +2,7 @@ package lime.media.openal;

#if (!lime_doc_gen || lime_openal)
import lime._internal.backend.native.NativeCFFI;
import lime.system.CFFI;
import lime.system.CFFIPointer;
import lime.utils.ArrayBufferView;

Expand Down Expand Up @@ -998,10 +999,7 @@ class AL
{
#if (lime_cffi && lime_openal && !macro)
var result = NativeCFFI.lime_al_get_string(param);
#if hl
var result = @:privateAccess String.fromUTF8(result);
#end
return result;
return CFFI.stringValue(result);
#else
return null;
#end
Expand Down
6 changes: 2 additions & 4 deletions src/lime/media/openal/ALC.hx
Expand Up @@ -2,6 +2,7 @@ package lime.media.openal;

#if (!lime_doc_gen || lime_openal)
import lime._internal.backend.native.NativeCFFI;
import lime.system.CFFI;
import lime.system.CFFIPointer;

#if !lime_debug
Expand Down Expand Up @@ -144,10 +145,7 @@ class ALC
{
#if (lime_cffi && lime_openal && !macro)
var result = NativeCFFI.lime_alc_get_string(device, param);
#if hl
var result = @:privateAccess String.fromUTF8(result);
#end
return result;
return CFFI.stringValue(result);
#else
return null;
#end
Expand Down
32 changes: 7 additions & 25 deletions src/lime/net/curl/CURL.hx
Expand Up @@ -3,6 +3,7 @@ package lime.net.curl;
#if (!lime_doc_gen || lime_curl)
import haxe.io.Bytes;
import lime._internal.backend.native.NativeCFFI;
import lime.system.CFFI;
import lime.system.CFFIPointer;

#if !lime_debug
Expand Down Expand Up @@ -57,10 +58,7 @@ class CURL
{
#if (lime_cffi && lime_curl && !macro)
var result = NativeCFFI.lime_curl_easy_escape(handle, url, length);
#if hl
var result = @:privateAccess String.fromUTF8(result);
#end
return result;
return CFFI.stringValue(result);
#else
return null;
#end
Expand Down Expand Up @@ -187,17 +185,10 @@ class CURL

case CURLOption.HEADERFUNCTION:
var callback:CURL->String->Void = cast parameter;
#if hl
parameter = function(header:hl.Bytes)
{
callback(this, @:privateAccess String.fromUTF8(header));
}
#else
parameter = function(header:String)
parameter = function(header)
{
callback(this, header);
callback(this, CFFI.stringValue(header));
}
#end

case CURLOption.HTTPHEADER:
#if hl
Expand All @@ -221,10 +212,7 @@ class CURL
{
#if (lime_cffi && lime_curl && !macro)
var result = NativeCFFI.lime_curl_easy_strerror(cast(code, Int));
#if hl
var result = @:privateAccess String.fromUTF8(result);
#end
return result;
return CFFI.stringValue(result);
#else
return null;
#end
Expand All @@ -234,10 +222,7 @@ class CURL
{
#if (lime_cffi && lime_curl && !macro)
var result = NativeCFFI.lime_curl_easy_unescape(handle, url, inLength, outLength);
#if hl
var result = @:privateAccess String.fromUTF8(result);
#end
return result;
return CFFI.stringValue(result);
#else
return null;
#end
Expand All @@ -247,10 +232,7 @@ class CURL
{
#if (lime_cffi && lime_curl && !macro)
var result = NativeCFFI.lime_curl_version();
#if hl
var result = @:privateAccess String.fromUTF8(result);
#end
return result;
return CFFI.stringValue(result);
#else
return null;
#end
Expand Down
9 changes: 9 additions & 0 deletions src/lime/system/CFFI.hx
Expand Up @@ -204,6 +204,15 @@ class CFFI
#end
}

@:dox(hide) #if !hl inline #end public static function stringValue(#if hl value:hl.Bytes #else value:String #end):String
{
#if hl
return value != null ? @:privateAccess String.fromUTF8(value) : null;
#else
return value;
#end
}

private static function __findHaxelib(library:String):String
{
#if (sys && !macro && !html5)
Expand Down
11 changes: 2 additions & 9 deletions src/lime/system/Clipboard.hx
Expand Up @@ -3,6 +3,7 @@ package lime.system;
import lime._internal.backend.native.NativeCFFI;
import lime.app.Application;
import lime.app.Event;
import lime.system.CFFI;
#if flash
import flash.desktop.Clipboard as FlashClipboard;
#elseif (js && html5)
Expand All @@ -28,15 +29,7 @@ class Clipboard
_text = null;

#if (lime_cffi && !macro)
#if hl
var utf = NativeCFFI.lime_clipboard_get_text();
if (utf != null)
{
_text = @:privateAccess String.fromUTF8(utf);
}
#else
_text = NativeCFFI.lime_clipboard_get_text();
#end
_text = CFFI.stringValue(NativeCFFI.lime_clipboard_get_text());
#elseif flash
if (FlashClipboard.generalClipboard.hasFormat(TEXT_FORMAT))
{
Expand Down
3 changes: 1 addition & 2 deletions src/lime/system/Locale.hx
Expand Up @@ -68,8 +68,7 @@ abstract Locale(String) from String to String
locale = toString(getDefault());
#elseif (lime_cffi && !macro)
#if hl
var _locale = lime_locale_get_system_locale();
locale = _locale != null ? @:privateAccess String.fromUTF8(_locale) : null;
locale = CFFI.stringValue(lime_locale_get_system_locale());
#else
locale = CFFI.load("lime", "lime_locale_get_system_locale", 0)();
#end
Expand Down

0 comments on commit 88c2f6d

Please sign in to comment.