From 139675cc05b7883ea9a07547cd2ab0a3fe06643c Mon Sep 17 00:00:00 2001 From: Nathan Moinvaziri Date: Thu, 21 Dec 2023 17:15:37 -0800 Subject: [PATCH 1/4] Add gitignore to ignore build directory. --- .gitignore | 1 + 1 file changed, 1 insertion(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..fcb6a2f --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +build From c2badd67580156e55abb049d4e73b8000075d7c1 Mon Sep 17 00:00:00 2001 From: Nathan Moinvaziri Date: Thu, 21 Dec 2023 17:16:58 -0800 Subject: [PATCH 2/4] Update interop library for DIRECT 7.2.13. --- lib/interop | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/interop b/lib/interop index b32cd47..5ccc250 160000 --- a/lib/interop +++ b/lib/interop @@ -1 +1 @@ -Subproject commit b32cd4745642303ab393b9c3b59ab14f692f750d +Subproject commit 5ccc2505bcefb7c540c44e0d78ca1cd8cc8b6f8c From 45de1bfbb28ab20ac5de1477d1d9cfead392f150 Mon Sep 17 00:00:00 2001 From: Nathan Moinvaziri Date: Thu, 21 Dec 2023 17:17:16 -0800 Subject: [PATCH 3/4] Update Simple object for DIRECT 7.2 changes. --- js/Simple.js | 16 ++++++++-------- js/SimpleExample.js | 8 ++++---- simple.c | 38 +++++++++++++++++++------------------- simple.h | 8 ++++---- 4 files changed, 35 insertions(+), 35 deletions(-) diff --git a/js/Simple.js b/js/Simple.js index eb8671f..0d50b00 100644 --- a/js/Simple.js +++ b/js/Simple.js @@ -36,17 +36,17 @@ Simple.prototype.releaseInstance = function() { * Gets an integer value * @type int */ -Simple.prototype.getInt64Property = function() { +Simple.prototype.getIntProperty = function() { return this.invoke({ - "method": "getInt64Property" + "method": "getIntProperty" }); }; /** * Sets an integer value */ -Simple.prototype.setInt64Property = function(value) { +Simple.prototype.setIntProperty = function(value) { return this.invoke({ - "method": "setInt64Property", + "method": "setIntProperty", "value": value }); }; @@ -54,17 +54,17 @@ Simple.prototype.setInt64Property = function(value) { * Gets a double value * @type float */ -Simple.prototype.getFloat64Property = function() { +Simple.prototype.getFloatProperty = function() { return this.invoke({ - "method": "getFloat64Property" + "method": "getFloatProperty" }); }; /** * Sets a double value */ -Simple.prototype.setFloat64Property = function(value) { +Simple.prototype.setFloatProperty = function(value) { return this.invoke({ - "method": "setFloat64Property", + "method": "setFloatProperty", "value": value }); }; diff --git a/js/SimpleExample.js b/js/SimpleExample.js index cc763cd..bb641d7 100644 --- a/js/SimpleExample.js +++ b/js/SimpleExample.js @@ -21,10 +21,10 @@ function interopLoaded() { // Calls from JS to C are synchronous console.log("Simple InstanceId - " + simple.instanceId); - simple.setInt64Property(406); - console.log("Simple Int64 - " + simple.getInt64Property()); - simple.setFloat64Property(40.1); - console.log("Simple Float64 - " + simple.getFloat64Property()); + simple.setIntProperty(406); + console.log("Simple Int - " + simple.getIntProperty()); + simple.setFloatProperty(40.1); + console.log("Simple Float - " + simple.getFloatProperty()); simple.setBooleanProperty(true); console.log("Simple Boolean - " + simple.getBooleanProperty()); simple.setStringProperty("String Test"); diff --git a/simple.c b/simple.c index e425fb5..7bb8aca 100644 --- a/simple.c +++ b/simple.c @@ -37,10 +37,10 @@ static bool Simple_Notification_OnUpdate(void *UserPtr, const char *Type, const if (IDictionary_GetStringPtrByKey(DictionaryHandle, "String", &ValuePtr) == true) Simple_SetStringProperty(Simple, ValuePtr); - if (IDictionary_GetFloat64ByKey(DictionaryHandle, "Float64", &ValueFloat) == true) - Simple_SetFloat64Property(Simple, ValueFloat); + if (IDictionary_GetFloatByKey(DictionaryHandle, "Float64", &ValueFloat) == true) + Simple_SetFloatProperty(Simple, ValueFloat); if (IDictionary_GetInt64ByKey(DictionaryHandle, "Int64", &ValueInt64) == true) - Simple_SetInt64Property(Simple, ValueInt64); + Simple_SetIntProperty(Simple, ValueInt64); if (IDictionary_GetBooleanByKey(DictionaryHandle, "Boolean", &ValueBool) == true) Simple_SetBooleanProperty(Simple, ValueBool); return true; @@ -62,7 +62,7 @@ static bool Simple_Notification_OnValueResponse(void *UserPtr, const char *Type, /********************************************************************/ // Concrete functions -bool Simple_SetInt64Property(void *SimpleContext, int64_t Property) { +bool Simple_SetIntProperty(void *SimpleContext, int64_t Property) { SimpleStruct *Simple = (SimpleStruct *)SimpleContext; int64_t OldProperty = Simple->Int64Property; @@ -74,13 +74,13 @@ bool Simple_SetInt64Property(void *SimpleContext, int64_t Property) { return true; } -bool Simple_GetInt64Property(void *SimpleContext, int64_t *Property) { +bool Simple_GetIntProperty(void *SimpleContext, int64_t *Property) { SimpleStruct *Simple = (SimpleStruct *)SimpleContext; *Property = Simple->Int64Property; return true; } -bool Simple_SetFloat64Property(void *SimpleContext, float64_t Property) { +bool Simple_SetFloatProperty(void *SimpleContext, float64_t Property) { SimpleStruct *Simple = (SimpleStruct *)SimpleContext; float64_t OldProperty = Simple->Float64Property; @@ -92,7 +92,7 @@ bool Simple_SetFloat64Property(void *SimpleContext, float64_t Property) { return true; } -bool Simple_GetFloat64Property(void *SimpleContext, float64_t *Property) { +bool Simple_GetFloatProperty(void *SimpleContext, float64_t *Property) { SimpleStruct *Simple = (SimpleStruct *)SimpleContext; *Property = Simple->Float64Property; return true; @@ -182,7 +182,7 @@ bool Simple_Invoke(void *SimpleContext, echandle MethodDictionaryHandle, echandl SimpleStruct *Simple = (SimpleStruct *)SimpleContext; echandle ItemHandle = NULL; - float64_t ValueFloat64 = 0; + float64_t ValueFloat = 0; int64_t Value64 = 0; int32_t RetVal = false; int32_t ReturnValue = false; @@ -196,19 +196,19 @@ bool Simple_Invoke(void *SimpleContext, echandle MethodDictionaryHandle, echandl if (strcmp(Method, "setInt64Property") == 0) { RetVal = IDictionary_GetInt64ByKey(MethodDictionaryHandle, "value", &Value64); if (RetVal == true) - ReturnValue = Simple_SetInt64Property(SimpleContext, Value64); + ReturnValue = Simple_SetIntProperty(SimpleContext, Value64); IDictionary_AddBoolean(ReturnDictionaryHandle, "returnValue", ReturnValue, &ItemHandle); - } else if (strcmp(Method, "getInt64Property") == 0) { - RetVal = Simple_GetInt64Property(Simple, &Value64); - IDictionary_AddInt64(ReturnDictionaryHandle, "returnValue", Value64, &ItemHandle); - } else if (strcmp(Method, "setFloat64Property") == 0) { - RetVal = IDictionary_GetFloat64ByKey(MethodDictionaryHandle, "value", &ValueFloat64); + } else if (strcmp(Method, "getIntProperty") == 0) { + RetVal = Simple_GetIntProperty(Simple, &Value64); + IDictionary_AddInt(ReturnDictionaryHandle, "returnValue", Value64, &ItemHandle); + } else if (strcmp(Method, "setFloatProperty") == 0) { + RetVal = IDictionary_GetFloatByKey(MethodDictionaryHandle, "value", &ValueFloat); if (RetVal == true) - ReturnValue = Simple_SetFloat64Property(SimpleContext, ValueFloat64); + ReturnValue = Simple_SetFloatProperty(SimpleContext, ValueFloat); IDictionary_AddBoolean(ReturnDictionaryHandle, "returnValue", ReturnValue, &ItemHandle); - } else if (strcmp(Method, "getFloat64Property") == 0) { - RetVal = Simple_GetFloat64Property(Simple, &ValueFloat64); - IDictionary_AddFloat64(ReturnDictionaryHandle, "returnValue", ValueFloat64, &ItemHandle); + } else if (strcmp(Method, "getFloatProperty") == 0) { + RetVal = Simple_GetFloatProperty(Simple, &ValueFloat); + IDictionary_AddFloat(ReturnDictionaryHandle, "returnValue", ValueFloat, &ItemHandle); } else if (strcmp(Method, "setBooleanProperty") == 0) { RetVal = IDictionary_GetBooleanByKey(MethodDictionaryHandle, "value", &ValueBool); if (RetVal == true) @@ -227,7 +227,7 @@ bool Simple_Invoke(void *SimpleContext, echandle MethodDictionaryHandle, echandl IDictionary_AddString(ReturnDictionaryHandle, "returnValue", ValueString, &ItemHandle); } else if (strcmp(Method, "startValueRequest") == 0) { RetVal = Simple_StartValueRequest(Simple); - IDictionary_AddInt64(ReturnDictionaryHandle, "returnValue", RetVal, &ItemHandle); + IDictionary_AddInt(ReturnDictionaryHandle, "returnValue", RetVal, &ItemHandle); } return RetVal; diff --git a/simple.h b/simple.h index ae47b15..84f38f9 100644 --- a/simple.h +++ b/simple.h @@ -9,11 +9,11 @@ extern "C" { /*********************************************************************/ -bool Simple_SetInt64Property(void *SimpleContext, int64_t Property); -bool Simple_GetInt64Property(void *SimpleContext, int64_t *Property); +bool Simple_SetIntProperty(void *SimpleContext, int64_t Property); +bool Simple_GetIntProperty(void *SimpleContext, int64_t *Property); -bool Simple_SetFloat64Property(void *SimpleContext, float64_t Property); -bool Simple_GetFloat64Property(void *SimpleContext, float64_t *Property); +bool Simple_SetFloatProperty(void *SimpleContext, float64_t Property); +bool Simple_GetFloatProperty(void *SimpleContext, float64_t *Property); bool Simple_SetBooleanProperty(void *SimpleContext, bool Property); bool Simple_GetBooleanProperty(void *SimpleContext, bool *Property); From 5eb00a174fef5470a4731b3cb49b764fb86f2381 Mon Sep 17 00:00:00 2001 From: Nathan Moinvaziri Date: Thu, 21 Dec 2023 17:23:53 -0800 Subject: [PATCH 4/4] Code simplification and cleanup for Simple object. --- interop.c | 4 +-- simple.c | 91 +++++++++++++++++++++---------------------------------- simple.h | 11 +++---- 3 files changed, 40 insertions(+), 66 deletions(-) diff --git a/interop.c b/interop.c index b0790c6..9edc57e 100644 --- a/interop.c +++ b/interop.c @@ -11,10 +11,8 @@ bool Interop_CreateInstance(const char *TypeName, char *InstanceId, int32_t Inst Interop_ExecuteCallback Execute, Interop_InvokeInstanceCallback *InvokeInstance, Interop_ReleaseInstanceCallback *ReleaseInstance, Interop_ProcessInstanceCallback *ProcessInstance, void **UserPtr) { - void *Context; - if (strcmp(TypeName, "SSN.Simple") == 0) { - Simple_Create(&Context); + void *Context = Simple_Create(); Simple_GetInstanceId(Context, InstanceId, InstanceIdLength); *InvokeInstance = Simple_Invoke; diff --git a/simple.c b/simple.c index 7bb8aca..447c3cb 100644 --- a/simple.c +++ b/simple.c @@ -18,8 +18,8 @@ typedef struct SimpleStruct { ClassStruct Class; // Object data - int64_t Int64Property; - float64_t Float64Property; + int64_t IntProperty; + float64_t FloatProperty; int32_t BooleanProperty; char StringProperty[320]; } SimpleStruct; @@ -37,9 +37,9 @@ static bool Simple_Notification_OnUpdate(void *UserPtr, const char *Type, const if (IDictionary_GetStringPtrByKey(DictionaryHandle, "String", &ValuePtr) == true) Simple_SetStringProperty(Simple, ValuePtr); - if (IDictionary_GetFloatByKey(DictionaryHandle, "Float64", &ValueFloat) == true) + if (IDictionary_GetFloatByKey(DictionaryHandle, "Float", &ValueFloat) == true) Simple_SetFloatProperty(Simple, ValueFloat); - if (IDictionary_GetInt64ByKey(DictionaryHandle, "Int64", &ValueInt64) == true) + if (IDictionary_GetInt64ByKey(DictionaryHandle, "Int", &ValueInt64) == true) Simple_SetIntProperty(Simple, ValueInt64); if (IDictionary_GetBooleanByKey(DictionaryHandle, "Boolean", &ValueBool) == true) Simple_SetBooleanProperty(Simple, ValueBool); @@ -64,38 +64,36 @@ static bool Simple_Notification_OnValueResponse(void *UserPtr, const char *Type, bool Simple_SetIntProperty(void *SimpleContext, int64_t Property) { SimpleStruct *Simple = (SimpleStruct *)SimpleContext; - int64_t OldProperty = Simple->Int64Property; + int64_t OldProperty = Simple->IntProperty; - Simple->Int64Property = Property; + Simple->IntProperty = Property; NotificationCenter_FireAfterDelayWithJSON("Simple", "Changed", Class_InstanceId(Simple), 0, - "{ \"newValue\": %lld, \"oldValue\": %lld }", Simple->Int64Property, + "{ \"newValue\": %lld, \"oldValue\": %lld }", Simple->IntProperty, OldProperty); return true; } -bool Simple_GetIntProperty(void *SimpleContext, int64_t *Property) { +int64_t Simple_GetIntProperty(void *SimpleContext) { SimpleStruct *Simple = (SimpleStruct *)SimpleContext; - *Property = Simple->Int64Property; - return true; + return Simple->IntProperty; } bool Simple_SetFloatProperty(void *SimpleContext, float64_t Property) { SimpleStruct *Simple = (SimpleStruct *)SimpleContext; - float64_t OldProperty = Simple->Float64Property; + float64_t OldProperty = Simple->FloatProperty; - Simple->Float64Property = Property; + Simple->FloatProperty = Property; NotificationCenter_FireAfterDelayWithJSON("Simple", "Changed", Class_InstanceId(Simple), 0, - "{ \"newValue\": %g, \"oldValue\": %g }", Simple->Float64Property, + "{ \"newValue\": %g, \"oldValue\": %g }", Simple->FloatProperty, OldProperty); return true; } -bool Simple_GetFloatProperty(void *SimpleContext, float64_t *Property) { +float64_t Simple_GetFloatProperty(void *SimpleContext) { SimpleStruct *Simple = (SimpleStruct *)SimpleContext; - *Property = Simple->Float64Property; - return true; + return Simple->FloatProperty; } bool Simple_SetBooleanProperty(void *SimpleContext, bool Property) { @@ -110,10 +108,9 @@ bool Simple_SetBooleanProperty(void *SimpleContext, bool Property) { return true; } -bool Simple_GetBooleanProperty(void *SimpleContext, bool *Property) { +bool Simple_GetBooleanProperty(void *SimpleContext) { SimpleStruct *Simple = (SimpleStruct *)SimpleContext; - *Property = Simple->BooleanProperty; - return true; + return Simple->BooleanProperty; } bool Simple_SetStringProperty(void *SimpleContext, const char *Property) { @@ -134,17 +131,9 @@ bool Simple_SetStringProperty(void *SimpleContext, const char *Property) { return true; } -bool Simple_GetStringProperty(void *SimpleContext, char *Property, int32_t MaxPropertyLength) { - SimpleStruct *Simple = (SimpleStruct *)SimpleContext; - strncpy(Property, Simple->StringProperty, MaxPropertyLength); - Property[MaxPropertyLength - 1] = 0; - return true; -} - -bool Simple_GetStringPropertyPtr(void *SimpleContext, const char **PropertyPtr) { +const char *Simple_GetStringProperty(void *SimpleContext) { SimpleStruct *Simple = (SimpleStruct *)SimpleContext; - *PropertyPtr = Simple->StringProperty; - return true; + return Simple->StringProperty; } bool Simple_StartValueRequest(void *SimpleContext) { @@ -182,48 +171,48 @@ bool Simple_Invoke(void *SimpleContext, echandle MethodDictionaryHandle, echandl SimpleStruct *Simple = (SimpleStruct *)SimpleContext; echandle ItemHandle = NULL; - float64_t ValueFloat = 0; - int64_t Value64 = 0; int32_t RetVal = false; int32_t ReturnValue = false; - bool ValueBool = 0; const char *Method = NULL; - const char *ValueString = NULL; if (IDictionary_GetStringPtrByKey(MethodDictionaryHandle, "method", &Method) == false) return false; if (strcmp(Method, "setInt64Property") == 0) { + int64_t Value64 = 0; RetVal = IDictionary_GetInt64ByKey(MethodDictionaryHandle, "value", &Value64); if (RetVal == true) ReturnValue = Simple_SetIntProperty(SimpleContext, Value64); IDictionary_AddBoolean(ReturnDictionaryHandle, "returnValue", ReturnValue, &ItemHandle); } else if (strcmp(Method, "getIntProperty") == 0) { - RetVal = Simple_GetIntProperty(Simple, &Value64); + int64_t Value64 = Simple_GetIntProperty(Simple); IDictionary_AddInt(ReturnDictionaryHandle, "returnValue", Value64, &ItemHandle); } else if (strcmp(Method, "setFloatProperty") == 0) { + float64_t ValueFloat = 0; RetVal = IDictionary_GetFloatByKey(MethodDictionaryHandle, "value", &ValueFloat); if (RetVal == true) ReturnValue = Simple_SetFloatProperty(SimpleContext, ValueFloat); IDictionary_AddBoolean(ReturnDictionaryHandle, "returnValue", ReturnValue, &ItemHandle); } else if (strcmp(Method, "getFloatProperty") == 0) { - RetVal = Simple_GetFloatProperty(Simple, &ValueFloat); + float64_t ValueFloat = Simple_GetFloatProperty(Simple); IDictionary_AddFloat(ReturnDictionaryHandle, "returnValue", ValueFloat, &ItemHandle); } else if (strcmp(Method, "setBooleanProperty") == 0) { + bool ValueBool = 0; RetVal = IDictionary_GetBooleanByKey(MethodDictionaryHandle, "value", &ValueBool); if (RetVal == true) ReturnValue = Simple_SetBooleanProperty(SimpleContext, ValueBool); IDictionary_AddBoolean(ReturnDictionaryHandle, "returnValue", ReturnValue, &ItemHandle); } else if (strcmp(Method, "getBooleanProperty") == 0) { - RetVal = Simple_GetBooleanProperty(Simple, &ValueBool); + bool ValueBool = Simple_GetBooleanProperty(Simple); IDictionary_AddBoolean(ReturnDictionaryHandle, "returnValue", ValueBool, &ItemHandle); } else if (strcmp(Method, "setStringProperty") == 0) { + const char *ValueString = NULL; RetVal = IDictionary_GetStringPtrByKey(MethodDictionaryHandle, "value", &ValueString); if (RetVal == true) ReturnValue = Simple_SetStringProperty(SimpleContext, ValueString); IDictionary_AddBoolean(ReturnDictionaryHandle, "returnValue", ReturnValue, &ItemHandle); } else if (strcmp(Method, "getStringProperty") == 0) { - RetVal = Simple_GetStringPropertyPtr(Simple, &ValueString); + const char *ValueString = Simple_GetStringProperty(Simple); IDictionary_AddString(ReturnDictionaryHandle, "returnValue", ValueString, &ItemHandle); } else if (strcmp(Method, "startValueRequest") == 0) { RetVal = Simple_StartValueRequest(Simple); @@ -236,27 +225,17 @@ bool Simple_Invoke(void *SimpleContext, echandle MethodDictionaryHandle, echandl /*********************************************************************/ // Creation/deletion functions -bool Simple_Create(void **SimpleContext) { - SimpleStruct *Simple = NULL; +void *Simple_Create(void) { + SimpleStruct *Simple = (SimpleStruct *)calloc(1, sizeof(SimpleStruct)); + if (!Simple) + return NULL; - Simple = (SimpleStruct *)malloc(sizeof(SimpleStruct)); - if (Simple == NULL) - return false; - memset(Simple, 0, sizeof(SimpleStruct)); Interop_GenerateInstanceId(Simple->Class.InstanceId, sizeof(Simple->Class.InstanceId)); Simple->Class.RefCount = 1; - Simple->Int64Property = 0; - Simple->Float64Property = 0.f; - Simple->BooleanProperty = false; - NotificationCenter_AddInstanceObserver("Simple", "Update", Class_InstanceId(Simple), Simple, Simple_Notification_OnUpdate); - - memset(Simple->StringProperty, 0, sizeof(Simple->StringProperty)); - - *SimpleContext = Simple; - return true; + return Simple; } void *Simple_AddRef(void *SimpleContext) { @@ -266,12 +245,10 @@ void *Simple_AddRef(void *SimpleContext) { } int32_t Simple_Release(void **SimpleContext) { - SimpleStruct *Simple; - - if (SimpleContext == NULL || *SimpleContext == NULL) + if (!SimpleContext || !*SimpleContext) return 0; - Simple = (SimpleStruct *)*SimpleContext; - if (Simple == NULL) + SimpleStruct *Simple = (SimpleStruct *)*SimpleContext; + if (!Simple) return 0; *SimpleContext = NULL; diff --git a/simple.h b/simple.h index 84f38f9..171d6b6 100644 --- a/simple.h +++ b/simple.h @@ -10,17 +10,16 @@ extern "C" { /*********************************************************************/ bool Simple_SetIntProperty(void *SimpleContext, int64_t Property); -bool Simple_GetIntProperty(void *SimpleContext, int64_t *Property); +int64_t Simple_GetIntProperty(void *SimpleContext); bool Simple_SetFloatProperty(void *SimpleContext, float64_t Property); -bool Simple_GetFloatProperty(void *SimpleContext, float64_t *Property); +float64_t Simple_GetFloatProperty(void *SimpleContext); bool Simple_SetBooleanProperty(void *SimpleContext, bool Property); -bool Simple_GetBooleanProperty(void *SimpleContext, bool *Property); +bool Simple_GetBooleanProperty(void *SimpleContext); bool Simple_SetStringProperty(void *SimpleContext, const char *Property); -bool Simple_GetStringProperty(void *SimpleContext, char *Property, int32_t MaxPropertyLength); -bool Simple_GetStringPropertyPtr(void *SimpleContext, const char **PropertyPtr); +const char *Simple_GetStringProperty(void *SimpleContext); bool Simple_StartValueRequest(void *SimpleContext); @@ -31,7 +30,7 @@ bool Simple_Invoke(void *SimpleContext, echandle MethodDictionaryHandle, echandl /*********************************************************************/ -bool Simple_Create(void **SimpleContext); +void *Simple_Create(void); void *Simple_AddRef(void *SimpleContext); int32_t Simple_Release(void **SimpleContext);