Skip to content
Browse files

Reorganised classes

Implemented HasProperty / SetProperty / GetProperty methods

Signed-off-by: Peter Nelson <peter@peterdn.com>
  • Loading branch information...
1 parent 33b2ff1 commit f0db7de63a01f16ee43172c88234a29a16b52d0a @peterdn peterdn committed
Showing with 157 additions and 71 deletions.
  1. +34 −37 JSCore/JSContext.cpp
  2. +6 −0 JSCore/JSContext.h
  3. +34 −2 JSCore/JSCore.vcproj
  4. +40 −7 JSCore/JSObject.cpp
  5. +9 −2 JSCore/JSObject.h
  6. +27 −22 JSCore/JSValue.cpp
  7. +3 −1 JSCore/JSValue.h
  8. +4 −0 JSCore/Stdafx.h
View
71 JSCore/JSContext.cpp
@@ -1,20 +1,22 @@
#include "stdafx.h"
+#include "JSCoreMarshal.h"
#include "JSValue.h"
+#include "JSObject.h"
#include "JSContext.h"
-WebKit::JSCore::JSContext::JSContext(JSContextRef context)
+JSContext::JSContext(JSContextRef context)
: _context(context)
{
}
-WebKit::JSCore::JSContext::JSContext(System::IntPtr context)
+JSContext::JSContext(IntPtr context)
: _context((JSContextRef) context.ToPointer())
{
}
-WebKit::JSCore::JSContext::JSContext(WebKit::Interop::IWebFrame ^ webFrame)
+JSContext::JSContext(WebKit::Interop::IWebFrame ^ webFrame)
{
::IWebFrame * unmgdFrame = (::IWebFrame *) Marshal::GetComInterfaceForObject(webFrame,
WebKit::Interop::IWebFrame::typeid).ToPointer();
@@ -22,46 +24,37 @@ WebKit::JSCore::JSContext::JSContext(WebKit::Interop::IWebFrame ^ webFrame)
_context = unmgdFrame->globalContext();
}
-WebKit::JSCore::JSContext::~JSContext()
+JSContext::~JSContext()
{
// TODO: clean up
}
-WebKit::JSCore::JSValue ^ WebKit::JSCore::JSContext::EvaluateScript(System::String ^ script)
+JSValue ^ JSContext::EvaluateScript(String ^ script)
{
return EvaluateScript(script, nullptr, nullptr, 0);
}
-WebKit::JSCore::JSValue ^ WebKit::JSCore::JSContext::EvaluateScript(System::String ^ script, System::Object ^ thisObject)
+JSValue ^ JSContext::EvaluateScript(String ^ script, Object ^ thisObject)
{
return EvaluateScript(script, thisObject, nullptr, 0);
}
-WebKit::JSCore::JSValue ^ WebKit::JSCore::JSContext::EvaluateScript(System::String ^ script, System::Object ^ thisObject,
- System::String ^ sourceUrl, int startingLineNumber)
+JSValue ^ JSContext::EvaluateScript(String ^ script, Object ^ thisObject,
+ String ^ sourceUrl, int startingLineNumber)
{
// TODO: lets not worry about exceptions just yet...
- JSStringRef jsScript = NULL;
- if (script != nullptr)
- {
- BSTR unmgdScript = (BSTR) Marshal::StringToBSTR(script).ToPointer();
- jsScript = JSStringCreateWithCharacters((const JSChar *)unmgdScript, wcslen(unmgdScript));
- Marshal::FreeBSTR(IntPtr(unmgdScript));
- }
+ JSStringRef jsScript = JSCoreMarshal::StringToJSString(script);
// TODO: marshal thisObject to JSObject
JSObjectRef jsObj = NULL;
- JSStringRef jsSrc = NULL;
- if (sourceUrl != nullptr)
- {
- BSTR unmgdSrc = (BSTR) Marshal::StringToBSTR(sourceUrl).ToPointer();
- jsSrc = JSStringCreateWithCharacters((const JSChar *)unmgdSrc, wcslen(unmgdSrc));
- Marshal::FreeBSTR(IntPtr(unmgdSrc));
- }
-
- JSValueRef result = JSEvaluateScript(_context, jsScript, jsObj, jsSrc, 0, NULL);
+ // TODO: handle nulls and stuff
+ JSStringRef jsSrc = JSCoreMarshal::StringToJSString(sourceUrl);
+
+ JSValueRef exception = NULL;
+
+ JSValueRef result = JSEvaluateScript(_context, jsScript, jsObj, jsSrc, 0, &exception);
JSValue ^ retval = result != NULL ? gcnew JSValue(this, result) : nullptr;
// clean up
@@ -73,64 +66,68 @@ WebKit::JSCore::JSValue ^ WebKit::JSCore::JSContext::EvaluateScript(System::Stri
return retval;
}
-bool WebKit::JSCore::JSContext::CheckScriptSyntax(System::String ^ script)
+bool JSContext::CheckScriptSyntax(String ^ script)
{
return false;
}
-bool WebKit::JSCore::JSContext::CheckScriptSyntax(System::String ^ script, System::Object ^ thisObject)
+bool JSContext::CheckScriptSyntax(String ^ script, Object ^ thisObject)
{
return false;
}
-bool WebKit::JSCore::JSContext::CheckScriptSyntax(System::String ^script, System::Object ^ thisObject,
- System::String ^sourceUrl, int startingLineNumber)
+bool JSContext::CheckScriptSyntax(String ^ script, Object ^ thisObject,
+ String ^ sourceUrl, int startingLineNumber)
{
return false;
}
-void WebKit::JSCore::JSContext::GarbageCollect()
+void JSContext::GarbageCollect()
{
JSGarbageCollect(_context);
}
-WebKit::JSCore::JSValue ^ WebKit::JSCore::JSContext::MakeUndefined()
+JSValue ^ JSContext::MakeUndefined()
{
return nullptr;
}
-WebKit::JSCore::JSValue ^ WebKit::JSCore::JSContext::MakeNull()
+JSValue ^ JSContext::MakeNull()
{
return nullptr;
}
-WebKit::JSCore::JSValue ^ WebKit::JSCore::JSContext::MakeBoolean(bool boolean)
+JSValue ^ JSContext::MakeBoolean(bool boolean)
{
return nullptr;
}
-WebKit::JSCore::JSValue ^ WebKit::JSCore::JSContext::MakeNumber(double number)
+JSValue ^ JSContext::MakeNumber(double number)
{
return nullptr;
}
-WebKit::JSCore::JSValue ^ WebKit::JSCore::JSContext::MakeString(System::String ^ string)
+JSValue ^ JSContext::MakeString(String ^ string)
{
return nullptr;
}
-WebKit::JSCore::JSValue ^ WebKit::JSCore::JSContext::MakeValueFromJSONString(System::String ^ jsonString)
+JSValue ^ JSContext::MakeValueFromJSONString(String ^ jsonString)
{
return nullptr;
}
-WebKit::JSCore::JSObject ^ WebKit::JSCore::JSContext::MakeObject(System::Object ^ object)
+JSObject ^ JSContext::MakeObject(Object ^ object)
{
return nullptr;
}
-JSContextRef WebKit::JSCore::JSContext::context()
+JSContextRef JSContext::context()
{
return _context;
}
+JSObject ^ JSContext::GetGlobalObject()
+{
+ return gcnew JSObject(this, ::JSContextGetGlobalObject(_context));
+}
View
6 JSCore/JSContext.h
@@ -3,12 +3,14 @@
using namespace System;
using namespace System::Runtime::InteropServices;
using namespace WebKit::Interop;
+using namespace WebKit::JSCore;
namespace WebKit {
namespace JSCore {
ref class JSValue;
ref class JSObject;
+class JSCoreMarshal;
public ref class JSContext
{
@@ -45,6 +47,10 @@ public ref class JSContext
JSObject ^ MakeObject(Object ^ object);
+ /* JSContextRef.h functions */
+
+ JSObject ^ GetGlobalObject();
+
internal:
JSContext(JSContextRef context);
View
36 JSCore/JSCore.vcproj
@@ -66,6 +66,7 @@
GenerateDebugInformation="true"
AssemblyDebug="1"
TargetMachine="1"
+ KeyFile="&quot;$(SolutionDir)WebKit.NET.snk&quot;"
/>
<Tool
Name="VCALinkTool"
@@ -100,6 +101,7 @@
>
<Tool
Name="VCPreBuildEventTool"
+ CommandLine=""
/>
<Tool
Name="VCCustomBuildTool"
@@ -138,6 +140,7 @@
AdditionalLibraryDirectories="$(SolutionDir)lib"
GenerateDebugInformation="true"
TargetMachine="1"
+ KeyFile="&quot;$(SolutionDir)WebKit.NET.snk&quot;"
/>
<Tool
Name="VCALinkTool"
@@ -178,6 +181,11 @@
AssemblyName="System.Xml, Version=2.0.0.0, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL"
MinFrameworkVersion="131072"
/>
+ <AssemblyReference
+ RelativePath="..\webkit\WebKit.Interop.dll"
+ AssemblyName="WebKit.Interop, Version=533.0.0.0, PublicKeyToken=b967213f6d29a3be, processorArchitecture=MSIL"
+ MinFrameworkVersion="0"
+ />
</References>
<Files>
<Filter
@@ -190,11 +198,23 @@
>
</File>
<File
+ RelativePath=".\JSContext.cpp"
+ >
+ </File>
+ <File
RelativePath=".\JSCore.cpp"
>
</File>
<File
- RelativePath=".\JSWrappers.cpp"
+ RelativePath=".\JSCoreMarshal.cpp"
+ >
+ </File>
+ <File
+ RelativePath=".\JSObject.cpp"
+ >
+ </File>
+ <File
+ RelativePath=".\JSValue.cpp"
>
</File>
<File
@@ -224,11 +244,23 @@
UniqueIdentifier="{93995380-89BD-4b04-88EB-625FBE52EBFB}"
>
<File
+ RelativePath=".\JSContext.h"
+ >
+ </File>
+ <File
RelativePath=".\JSCore.h"
>
</File>
<File
- RelativePath=".\JSWrappers.h"
+ RelativePath=".\JSCoreMarshal.h"
+ >
+ </File>
+ <File
+ RelativePath=".\JSObject.h"
+ >
+ </File>
+ <File
+ RelativePath=".\JSValue.h"
>
</File>
<File
View
47 JSCore/JSObject.cpp
@@ -2,24 +2,57 @@
#include "JSValue.h"
#include "JSObject.h"
+#include "JSContext.h"
+#include "JSCoreMarshal.h"
-WebKit::JSCore::JSObject::JSObject(JSContext ^ context, System::Object ^object)
-: WebKit::JSCore::JSValue(context, NULL)
+JSObject::JSObject(JSContext ^ context, JSObjectRef object)
+: JSValue(context, (JSValueRef)object)
{
}
-bool WebKit::JSCore::JSObject::HasProperty(System::String ^propertyName)
+bool JSObject::HasProperty(String ^ propertyName)
{
- return false;
+ JSStringRef str = JSCoreMarshal::StringToJSString(propertyName);
+ bool val = JSObjectHasProperty(_context->context(), (JSObjectRef)_value, str);
+ JSStringRelease(str);
+ return val;
}
-WebKit::JSCore::JSValue ^ WebKit::JSCore::JSObject::GetProperty(System::String ^ propertyName)
+JSValue ^ JSObject::GetProperty(String ^ propertyName)
{
- return nullptr;
+ JSStringRef str = JSCoreMarshal::StringToJSString(propertyName);
+
+ JSValueRef val = JSObjectGetProperty(_context->context(), (JSObjectRef)_value, str, NULL);
+
+ JSStringRelease(str);
+ return gcnew JSValue(_context, val);
+}
+
+void JSObject::SetProperty(String ^ propertyName, bool value)
+{
+ SetProperty(propertyName, JSValueMakeBoolean(_context->context(), value));
+}
+
+void JSObject::SetProperty(String ^ propertyName, double value)
+{
+ SetProperty(propertyName, JSValueMakeNumber(_context->context(), value));
}
-void WebKit::JSCore::JSObject::SetProperty(System::String ^ propertyName, int value)
+void JSObject::SetProperty(String ^ propertyName, System::Object ^ value)
{
+}
+void JSObject::SetProperty(String ^ propertyName, System::String ^ value)
+{
+ JSStringRef jsStr = JSCoreMarshal::StringToJSString(value);
+ SetProperty(propertyName, JSValueMakeString(_context->context(), jsStr));
+ JSStringRelease(jsStr);
}
+
+void JSObject::SetProperty(String ^ propertyName, JSValueRef value)
+{
+ JSStringRef jsStr = JSCoreMarshal::StringToJSString(propertyName);
+ JSObjectSetProperty(_context->context(), (JSObjectRef)_value, jsStr, value, NULL, NULL);
+ JSStringRelease(jsStr);
+}
View
11 JSCore/JSObject.h
@@ -3,6 +3,7 @@
using namespace System;
using namespace System::Runtime::InteropServices;
using namespace WebKit::Interop;
+using namespace WebKit::JSCore;
namespace WebKit {
namespace JSCore {
@@ -15,10 +16,16 @@ public ref class JSObject : public WebKit::JSCore::JSValue
public:
bool HasProperty(String ^ propertyName);
JSValue ^ GetProperty(String ^ propertyName);
- void SetProperty(String ^ propertyName, int value);
+ void SetProperty(String ^ propertyName, bool value);
+ void SetProperty(String ^ propertyName, double value);
+ void SetProperty(String ^ propertyName, System::Object ^ value);
+ void SetProperty(String ^ propertyName, System::String ^ value);
internal:
- JSObject(JSContext ^ context, Object ^ object);
+ JSObject(JSContext ^ context, JSObjectRef object);
+
+private:
+ void SetProperty(String ^ propertyName, JSValueRef value);
};
View
49 JSCore/JSValue.cpp
@@ -1,84 +1,89 @@
#include "stdafx.h"
+#include "JSCoreMarshal.h"
#include "JSContext.h"
#include "JSValue.h"
+#include "JSObject.h"
-WebKit::JSCore::JSValue::JSValue(JSContext ^ context, JSValueRef value)
+JSValue::JSValue(JSContext ^ context, JSValueRef value)
: _context(context), _value(value)
{
+ // TODO: is this necessary (probably...)?
+ JSValueProtect(_context->context(), _value);
}
-WebKit::JSCore::JSValue::~JSValue()
+JSValue::~JSValue()
{
+ // TODO: would probably be nicer to implement IDisposable instead
+ // erm......
+ if (_context != nullptr && _context->context() != NULL)
+ JSValueUnprotect(_context->context(), _value);
}
-String ^ WebKit::JSCore::JSValue::ToString()
+String ^ JSValue::ToString()
{
JSStringRef jsStr = JSValueToStringCopy(_context->context(), _value, NULL);
- int len = JSStringGetLength(jsStr);
- JSChar * cStr = (JSChar *) JSStringGetCharactersPtr(jsStr);
- cStr[len] = L'\0';
-
- // TODO: clean up
- return Marshal::PtrToStringAuto(IntPtr((void *) cStr));
+ String ^ str = JSCoreMarshal::JSStringToString(jsStr);
+ JSStringRelease(jsStr);
+ return str;
}
-bool WebKit::JSCore::JSValue::IsBoolean::get()
+bool JSValue::IsBoolean::get()
{
return JSValueIsBoolean(_context->context(), _value);
}
-bool WebKit::JSCore::JSValue::IsNull::get()
+bool JSValue::IsNull::get()
{
return JSValueIsNull(_context->context(), _value);
}
-bool WebKit::JSCore::JSValue::IsNumber::get()
+bool JSValue::IsNumber::get()
{
return JSValueIsNumber(_context->context(), _value);
}
-bool WebKit::JSCore::JSValue::IsObject::get()
+bool JSValue::IsObject::get()
{
return JSValueIsObject(_context->context(), _value);
}
-bool WebKit::JSCore::JSValue::IsString::get()
+bool JSValue::IsString::get()
{
return JSValueIsString(_context->context(), _value);
}
-bool WebKit::JSCore::JSValue::IsUndefined::get()
+bool JSValue::IsUndefined::get()
{
return JSValueIsUndefined(_context->context(), _value);
}
-System::String ^ WebKit::JSCore::JSValue::ToJSONString()
+String ^ JSValue::ToJSONString()
{
return "";
}
-bool WebKit::JSCore::JSValue::ToBoolean()
+bool JSValue::ToBoolean()
{
return JSValueToBoolean(_context->context(), _value);
}
-double WebKit::JSCore::JSValue::ToNumber()
+double JSValue::ToNumber()
{
return JSValueToNumber(_context->context(), _value, NULL);
}
-System::Object ^ WebKit::JSCore::JSValue::ToObject()
+JSObject ^ JSValue::ToObject()
{
- return nullptr;
+ return gcnew JSObject(_context, (JSObjectRef)_value);
}
-void WebKit::JSCore::JSValue::Protect()
+void JSValue::Protect()
{
}
-void WebKit::JSCore::JSValue::Unprotect()
+void JSValue::Unprotect()
{
}
View
4 JSCore/JSValue.h
@@ -3,11 +3,13 @@
using namespace System;
using namespace System::Runtime::InteropServices;
using namespace WebKit::Interop;
+using namespace WebKit::JSCore;
namespace WebKit {
namespace JSCore {
ref class JSContext;
+ref class JSObject;
public ref class JSValue
{
@@ -34,7 +36,7 @@ public ref class JSValue
String ^ ToJSONString();
bool ToBoolean();
double ToNumber();
- Object ^ ToObject();
+ JSObject ^ ToObject();
void Protect();
void Unprotect();
View
4 JSCore/Stdafx.h
@@ -6,3 +6,7 @@
#include <JavaScriptCore/JavaScriptCore.h>
#include <WebKit/WebKit.h>
+
+namespace WebKit {
+namespace JSCore {
+}}

0 comments on commit f0db7de

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