Skip to content
This repository
Browse code

Implemented wrapper_GetProperty, wrapper_SetProperty, added stub for …

…wrapper_CallAsFunction

Signed-off-by: Peter Nelson <peter@peterdn.com>
  • Loading branch information...
commit 7993ce92e800efb61517ee234488ba1116cc155a 1 parent 7213306
Peter Nelson peterdn authored
121 JSCore/JSCoreObjectWrapper.cpp
... ... @@ -1,55 +1,122 @@
1 1 #include "stdafx.h"
2 2
3 3 #include "JSCoreObjectWrapper.h"
  4 +#include "JSCoreMarshal.h"
4 5
5 6
6 7 JSClassDefinition wrapperClass =
7 8 {
8   - 0, /* int version; */
9   - kJSClassAttributeNone, /* JSClassAttributes attributes; */
  9 + 0, /* int version; */
  10 + kJSClassAttributeNone, /* JSClassAttributes attributes; */
10 11
11   - "wrapper", /* const char* className; */
12   - NULL, /* JSClassRef parentClass; */
  12 + "wrapper", /* const char* className; */
  13 + NULL, /* JSClassRef parentClass; */
13 14
14   - NULL, /* const JSStaticValue* staticValues; */
15   - NULL, /* const JSStaticFunction* staticFunctions; */
  15 + NULL, /* const JSStaticValue* staticValues; */
  16 + NULL, /* const JSStaticFunction* staticFunctions; */
16 17
17   - NULL, /* JSObjectInitializeCallback initialize; */
18   - NULL, /* JSObjectFinalizeCallback finalize; */
19   - wrapper_HasProperty, /* JSObjectHasPropertyCallback hasProperty; */
20   - wrapper_GetProperty, /* JSObjectGetPropertyCallback getProperty; */
21   - wrapper_SetProperty /* JSObjectSetPropertyCallback setProperty; */
22   - /* JSObjectDeletePropertyCallback deleteProperty; */
23   - /* JSObjectGetPropertyNamesCallback getPropertyNames; */
24   - /* JSObjectCallAsFunctionCallback callAsFunction; */
25   - /* JSObjectCallAsConstructorCallback callAsConstructor; */
26   - /* JSObjectHasInstanceCallback hasInstance; */
27   - /* JSObjectConvertToTypeCallback convertToType; */
  18 + NULL, /* JSObjectInitializeCallback initialize; */
  19 + NULL, /* JSObjectFinalizeCallback finalize; */
  20 + wrapper_HasProperty, /* JSObjectHasPropertyCallback hasProperty; */
  21 + wrapper_GetProperty, /* JSObjectGetPropertyCallback getProperty; */
  22 + wrapper_SetProperty, /* JSObjectSetPropertyCallback setProperty; */
  23 + NULL, /* JSObjectDeletePropertyCallback deleteProperty; */
  24 + NULL, /* JSObjectGetPropertyNamesCallback getPropertyNames; */
  25 + wrapper_CallAsFunction /* JSObjectCallAsFunctionCallback callAsFunction; */
  26 + /* JSObjectCallAsConstructorCallback callAsConstructor; */
  27 + /* JSObjectHasInstanceCallback hasInstance; */
  28 + /* JSObjectConvertToTypeCallback convertToType; */
28 29 };
29 30
30   -bool wrapper_HasProperty(JSContextRef ctx, JSObjectRef object, JSStringRef propertyName)
31   -{
  31 +Object ^ getObjectFromJSObjectRef(JSObjectRef object)
  32 +{
32 33 void * ptr = JSObjectGetPrivate(object);
33 34 GCHandle handle = GCHandle::FromIntPtr(IntPtr(ptr));
34   - Object ^ obj = handle.Target;
  35 + return handle.Target;
  36 +}
35 37
36   - // TODO: reflection magicks
  38 +bool wrapper_HasProperty(JSContextRef ctx, JSObjectRef object, JSStringRef propertyName)
  39 +{
  40 + Object ^ obj = getObjectFromJSObjectRef(object);
37 41
38   - return false;
  42 + String ^ propName = JSCoreMarshal::JSStringToString(propertyName);
  43 + return obj->GetType()->GetProperty(propName) != nullptr;
39 44 }
40 45
41 46 JSValueRef wrapper_GetProperty(JSContextRef ctx, JSObjectRef object, JSStringRef propertyName, JSValueRef* exception)
42 47 {
43   - void * ptr = JSObjectGetPrivate(object);
44   - GCHandle handle = GCHandle::FromIntPtr(IntPtr(ptr));
45   - Object ^ obj = handle.Target;
  48 + Object ^ obj = getObjectFromJSObjectRef(object);
46 49
47   - // TODO: reflection magicks
  50 + String ^ propName = JSCoreMarshal::JSStringToString(propertyName);
  51 + PropertyInfo ^ prop = obj->GetType()->GetProperty(propName);
  52 + if (prop != nullptr)
  53 + {
  54 + // for the moment, just return as string
  55 + if (prop->CanRead)
  56 + {
  57 + String ^ val = prop->GetValue(obj, nullptr)->ToString();
  58 + JSStringRef jsStr = JSCoreMarshal::StringToJSString(val);
  59 + return JSValueMakeString(ctx, jsStr);
  60 + }
  61 + else
  62 + {
  63 + // exception?
  64 + }
  65 + }
48 66
49 67 return NULL;
50 68 }
51 69
52   -bool wrapper_SetProperty(JSContextRef ctx, JSObjectRef object, JSStringRef propertyName, JSValueRef value, JSValueRef* exception)
  70 +bool wrapper_SetProperty(JSContextRef ctx, JSObjectRef object,
  71 + JSStringRef propertyName, JSValueRef value, JSValueRef* exception)
53 72 {
  73 + Object ^ obj = getObjectFromJSObjectRef(object);
  74 +
  75 + String ^ propName = JSCoreMarshal::JSStringToString(propertyName);
  76 + PropertyInfo ^ prop = obj->GetType()->GetProperty(propName);
  77 + if (prop != nullptr)
  78 + {
  79 + if (prop->CanWrite)
  80 + {
  81 + Type ^ type = prop->PropertyType;
  82 + Object ^ val;
  83 + JSValueRef exception = NULL;
  84 +
  85 + if (type == double::typeid)
  86 + {
  87 + val = JSValueToNumber(ctx, value, &exception);
  88 + }
  89 + else if (type == bool::typeid)
  90 + {
  91 + val = JSValueToBoolean(ctx, value);
  92 + }
  93 + else if (type == String::typeid)
  94 + {
  95 + JSStringRef temp = JSValueToStringCopy(ctx, value, &exception);
  96 + if (!exception)
  97 + {
  98 + val = JSCoreMarshal::JSStringToString(temp);
  99 + JSStringRelease(temp);
  100 + }
  101 + }
  102 +
  103 + if (!exception)
  104 + {
  105 + prop->SetValue(obj, val, nullptr);
  106 + return true;
  107 + }
  108 + }
  109 + else
  110 + {
  111 + // exception?
  112 + }
  113 + }
  114 +
54 115 return false;
55 116 }
  117 +
  118 +JSValueRef wrapper_CallAsFunction (JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject,
  119 + size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
  120 +{
  121 + return NULL;
  122 +}
6 JSCore/JSCoreObjectWrapper.h
@@ -2,9 +2,13 @@
2 2
3 3 using namespace System;
4 4 using namespace System::Runtime::InteropServices;
  5 +using namespace System::Reflection;
5 6
6 7 extern JSClassDefinition wrapperClass;
7 8
8 9 bool wrapper_HasProperty(JSContextRef ctx, JSObjectRef object, JSStringRef propertyName);
9 10 JSValueRef wrapper_GetProperty(JSContextRef ctx, JSObjectRef object, JSStringRef propertyName, JSValueRef* exception);
10   -bool wrapper_SetProperty(JSContextRef ctx, JSObjectRef object, JSStringRef propertyName, JSValueRef value, JSValueRef* exception);
  11 +bool wrapper_SetProperty(JSContextRef ctx, JSObjectRef object, JSStringRef propertyName, JSValueRef value,
  12 + JSValueRef* exception);
  13 +JSValueRef wrapper_CallAsFunction (JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject,
  14 + size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception);

0 comments on commit 7993ce9

Please sign in to comment.
Something went wrong with that request. Please try again.