Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Simple object for DIRECT 7.2 changes. #30

Merged
merged 4 commits into from
Dec 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
build
4 changes: 1 addition & 3 deletions interop.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
16 changes: 8 additions & 8 deletions js/Simple.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,35 +36,35 @@ 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
});
};
/**
* 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
});
};
Expand Down
8 changes: 4 additions & 4 deletions js/SimpleExample.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
2 changes: 1 addition & 1 deletion lib/interop
117 changes: 47 additions & 70 deletions simple.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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_GetInt64ByKey(DictionaryHandle, "Int64", &ValueInt64) == true)
Simple_SetInt64Property(Simple, ValueInt64);
if (IDictionary_GetFloatByKey(DictionaryHandle, "Float", &ValueFloat) == true)
Simple_SetFloatProperty(Simple, ValueFloat);
if (IDictionary_GetInt64ByKey(DictionaryHandle, "Int", &ValueInt64) == true)
Simple_SetIntProperty(Simple, ValueInt64);
if (IDictionary_GetBooleanByKey(DictionaryHandle, "Boolean", &ValueBool) == true)
Simple_SetBooleanProperty(Simple, ValueBool);
return true;
Expand All @@ -62,40 +62,38 @@ 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;
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_GetInt64Property(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_SetFloat64Property(void *SimpleContext, float64_t Property) {
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_GetFloat64Property(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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -182,52 +171,52 @@ bool Simple_Invoke(void *SimpleContext, echandle MethodDictionaryHandle, echandl

SimpleStruct *Simple = (SimpleStruct *)SimpleContext;
echandle ItemHandle = NULL;
float64_t ValueFloat64 = 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_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) {
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_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) {
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);
IDictionary_AddInt64(ReturnDictionaryHandle, "returnValue", RetVal, &ItemHandle);
IDictionary_AddInt(ReturnDictionaryHandle, "returnValue", RetVal, &ItemHandle);
}

return RetVal;
Expand All @@ -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) {
Expand All @@ -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;
Expand Down
15 changes: 7 additions & 8 deletions simple.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,17 @@ 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);
int64_t Simple_GetIntProperty(void *SimpleContext);

bool Simple_SetFloat64Property(void *SimpleContext, float64_t Property);
bool Simple_GetFloat64Property(void *SimpleContext, float64_t *Property);
bool Simple_SetFloatProperty(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);

Expand All @@ -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);

Expand Down
Loading