From 2946ba7b9a7b9cd3720865e7abe4171ec0816509 Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Wed, 23 Jan 2013 13:15:51 -0700 Subject: [PATCH] Convert `OnigRegExp` from a v8 extension to a window binding --- native/atom_cef_render_process_handler.mm | 2 +- native/v8_extensions/onig_reg_exp.h | 32 ++++++++++++++--------- native/v8_extensions/onig_reg_exp.js | 19 -------------- native/v8_extensions/onig_reg_exp.mm | 30 ++++++++++++++++----- src/app/window.coffee | 1 + src/stdlib/onig-reg-exp.coffee | 10 +++++++ 6 files changed, 55 insertions(+), 39 deletions(-) delete mode 100644 native/v8_extensions/onig_reg_exp.js create mode 100644 src/stdlib/onig-reg-exp.coffee diff --git a/native/atom_cef_render_process_handler.mm b/native/atom_cef_render_process_handler.mm index 8bce47601..ac4c8e877 100644 --- a/native/atom_cef_render_process_handler.mm +++ b/native/atom_cef_render_process_handler.mm @@ -10,7 +10,6 @@ #include void AtomCefRenderProcessHandler::OnWebKitInitialized() { - new v8_extensions::OnigRegExp(); new v8_extensions::OnigScanner(); new v8_extensions::Tags(); } @@ -21,6 +20,7 @@ void AtomCefRenderProcessHandler::OnContextCreated(CefRefPtr browser v8_extensions::Atom::CreateContextBinding(context); v8_extensions::Native::CreateContextBinding(context); v8_extensions::Git::CreateContextBinding(context); + v8_extensions::OnigRegExp::CreateContextBinding(context); } void AtomCefRenderProcessHandler::OnContextReleased(CefRefPtr browser, diff --git a/native/v8_extensions/onig_reg_exp.h b/native/v8_extensions/onig_reg_exp.h index 4a1725c48..1ae23b99c 100644 --- a/native/v8_extensions/onig_reg_exp.h +++ b/native/v8_extensions/onig_reg_exp.h @@ -3,18 +3,24 @@ namespace v8_extensions { -class OnigRegExp : public CefV8Handler { -public: - OnigRegExp(); - - virtual bool Execute(const CefString& name, - CefRefPtr object, - const CefV8ValueList& arguments, - CefRefPtr& retval, - CefString& exception) OVERRIDE; - - // Provide the reference counting implementation for this class. - IMPLEMENT_REFCOUNTING(OnigRegExp); -}; + class OnigRegExp : public CefV8Handler { + public: + static void CreateContextBinding(CefRefPtr context); + virtual bool Execute(const CefString& name, + CefRefPtr object, + const CefV8ValueList& arguments, + CefRefPtr& retval, + CefString& exception) OVERRIDE; + + // Provide the reference counting implementation for this class. + IMPLEMENT_REFCOUNTING(OnigRegExp); + + private: + static CefRefPtr GetInstance(); + OnigRegExp(); + OnigRegExp(OnigRegExp const&); + void operator=(OnigRegExp const&); + std::string windowState; + }; } \ No newline at end of file diff --git a/native/v8_extensions/onig_reg_exp.js b/native/v8_extensions/onig_reg_exp.js deleted file mode 100644 index ae06d5e36..000000000 --- a/native/v8_extensions/onig_reg_exp.js +++ /dev/null @@ -1,19 +0,0 @@ -(function() { - native function buildOnigRegExp(source); - native function search(string, index); - native function test(string); - - function OnigRegExp(source) { - var regexp = buildOnigRegExp(source); - regexp.constructor = OnigRegExp; - regexp.__proto__ = OnigRegExp.prototype; - regexp.source = source; - return regexp; - } - - OnigRegExp.prototype.search = search; - OnigRegExp.prototype.test = test; - - this.OnigRegExp = OnigRegExp; -})(); - diff --git a/native/v8_extensions/onig_reg_exp.mm b/native/v8_extensions/onig_reg_exp.mm index ba1657b1f..02a83d80b 100644 --- a/native/v8_extensions/onig_reg_exp.mm +++ b/native/v8_extensions/onig_reg_exp.mm @@ -38,21 +38,39 @@ public: return resultArray; } - + CefRefPtr Test(CefRefPtr string, CefRefPtr index) { OnigResult *result = [m_regex search:stringFromCefV8Value(string) start:index->GetIntValue()]; return CefV8Value::CreateBool(result); } - + OnigRegexp *m_regex; IMPLEMENT_REFCOUNTING(OnigRegexpUserData); }; OnigRegExp::OnigRegExp() : CefV8Handler() { - NSString *filePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"v8_extensions/onig_reg_exp.js"]; - NSString *extensionCode = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil]; - CefRegisterExtension("v8/onig-reg-exp", [extensionCode UTF8String], this); +} + +void OnigRegExp::CreateContextBinding(CefRefPtr context) { + const char* methodNames[] = { "search", "test", "buildOnigRegExp" }; + + CefRefPtr nativeObject = CefV8Value::CreateObject(NULL); + int arrayLength = sizeof(methodNames) / sizeof(const char *); + for (int i = 0; i < arrayLength; i++) { + const char *functionName = methodNames[i]; + CefRefPtr function = CefV8Value::CreateFunction(functionName, GetInstance()); + nativeObject->SetValue(functionName, function, V8_PROPERTY_ATTRIBUTE_NONE); + } + + CefRefPtr global = context->GetGlobal(); + global->SetValue("$onigRegExp", nativeObject, V8_PROPERTY_ATTRIBUTE_NONE); +} + +CefRefPtr OnigRegExp::GetInstance() { + static OnigRegExp instance; + static CefRefPtr instancePtr = CefRefPtr(&instance); + return instancePtr; } bool OnigRegExp::Execute(const CefString& name, @@ -73,7 +91,7 @@ bool OnigRegExp::Execute(const CefString& name, CefRefPtr index = arguments.size() > 1 ? arguments[1] : CefV8Value::CreateInt(0); OnigRegExpUserData *userData = (OnigRegExpUserData *)object->GetUserData().get(); retval = userData->Test(string, index); - return true; + return true; } else if (name == "buildOnigRegExp") { CefRefPtr pattern = arguments[0]; diff --git a/src/app/window.coffee b/src/app/window.coffee index d990b28a4..46612ff28 100644 --- a/src/app/window.coffee +++ b/src/app/window.coffee @@ -11,6 +11,7 @@ Pasteboard = require 'pasteboard' require 'jquery-extensions' require 'underscore-extensions' require 'space-pen-extensions' +require 'onig-reg-exp' windowAdditions = rootViewParentSelector: 'body' diff --git a/src/stdlib/onig-reg-exp.coffee b/src/stdlib/onig-reg-exp.coffee new file mode 100644 index 000000000..21cb58cdd --- /dev/null +++ b/src/stdlib/onig-reg-exp.coffee @@ -0,0 +1,10 @@ +class window.OnigRegExp + constructor: (source) -> + regexp = $onigRegExp.buildOnigRegExp(source); + regexp.constructor = OnigRegExp + regexp.__proto__ = OnigRegExp.prototype + regexp.source = source + return regexp + + search: $onigRegExp.search + test: $onigRegExp.test