Convert OnigRegExp from a v8 extension to a window binding

This commit is contained in:
Nathan Sobo 2013-01-23 13:15:51 -07:00 committed by Kevin Sawicki
parent 74d7d48dcc
commit 2946ba7b9a
6 changed files with 55 additions and 39 deletions

View File

@ -10,7 +10,6 @@
#include <iostream>
void AtomCefRenderProcessHandler::OnWebKitInitialized() {
new v8_extensions::OnigRegExp();
new v8_extensions::OnigScanner();
new v8_extensions::Tags();
}
@ -21,6 +20,7 @@ void AtomCefRenderProcessHandler::OnContextCreated(CefRefPtr<CefBrowser> 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<CefBrowser> browser,

View File

@ -3,18 +3,24 @@
namespace v8_extensions {
class OnigRegExp : public CefV8Handler {
public:
OnigRegExp();
virtual bool Execute(const CefString& name,
CefRefPtr<CefV8Value> object,
const CefV8ValueList& arguments,
CefRefPtr<CefV8Value>& retval,
CefString& exception) OVERRIDE;
// Provide the reference counting implementation for this class.
IMPLEMENT_REFCOUNTING(OnigRegExp);
};
class OnigRegExp : public CefV8Handler {
public:
static void CreateContextBinding(CefRefPtr<CefV8Context> context);
virtual bool Execute(const CefString& name,
CefRefPtr<CefV8Value> object,
const CefV8ValueList& arguments,
CefRefPtr<CefV8Value>& retval,
CefString& exception) OVERRIDE;
// Provide the reference counting implementation for this class.
IMPLEMENT_REFCOUNTING(OnigRegExp);
private:
static CefRefPtr<CefV8Handler> GetInstance();
OnigRegExp();
OnigRegExp(OnigRegExp const&);
void operator=(OnigRegExp const&);
std::string windowState;
};
}

View File

@ -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;
})();

View File

@ -38,21 +38,39 @@ public:
return resultArray;
}
CefRefPtr<CefV8Value> Test(CefRefPtr<CefV8Value> string, CefRefPtr<CefV8Value> 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<CefV8Context> context) {
const char* methodNames[] = { "search", "test", "buildOnigRegExp" };
CefRefPtr<CefV8Value> nativeObject = CefV8Value::CreateObject(NULL);
int arrayLength = sizeof(methodNames) / sizeof(const char *);
for (int i = 0; i < arrayLength; i++) {
const char *functionName = methodNames[i];
CefRefPtr<CefV8Value> function = CefV8Value::CreateFunction(functionName, GetInstance());
nativeObject->SetValue(functionName, function, V8_PROPERTY_ATTRIBUTE_NONE);
}
CefRefPtr<CefV8Value> global = context->GetGlobal();
global->SetValue("$onigRegExp", nativeObject, V8_PROPERTY_ATTRIBUTE_NONE);
}
CefRefPtr<CefV8Handler> OnigRegExp::GetInstance() {
static OnigRegExp instance;
static CefRefPtr<CefV8Handler> instancePtr = CefRefPtr<CefV8Handler>(&instance);
return instancePtr;
}
bool OnigRegExp::Execute(const CefString& name,
@ -73,7 +91,7 @@ bool OnigRegExp::Execute(const CefString& name,
CefRefPtr<CefV8Value> 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<CefV8Value> pattern = arguments[0];

View File

@ -11,6 +11,7 @@ Pasteboard = require 'pasteboard'
require 'jquery-extensions'
require 'underscore-extensions'
require 'space-pen-extensions'
require 'onig-reg-exp'
windowAdditions =
rootViewParentSelector: 'body'

View File

@ -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