August 21, 2007

Google CTemplate Integration (Check!)

Filed under: Distribute — Ryan Wilcox @ 5:39 pm

Today I tried out Google CTemplate, as I said I would. In addition to what I liked about the template language, it has tools to check your templates and generate C constants for the template elements (aka: the things to replace in the template). Meaning (if you use those constants) you can change the name of things in your template and your C++ code will complain, catching the errors at compile time instead of runtime.

I’ve integrated these tools into the build script so we have this safety from the get go.

I also have very comprehensive instructions for building the project, between gathering up all the tools to build on my functional machine, and fighting through all the build/linker errors I’ve been faced with.

So today my research app builds again. It’s a good day (and about time).

Back to full capacity

Filed under: Distribute,General Information,VonTrapp — Ryan Wilcox @ 10:11 am

For the last 2 weeks I’ve been down to only one machine, from my usual two development machines. Mishap with the power connector when replacing the hard drive in my iBook. Thanks to the good folks at DT&T Computer Services (who fixed, vs replaced, my motherboard), my machine is back up and running. Service from them was great (although they were having email sending issues so I had to call them to get my RMA number, and it seems like you have to send them an email to check the status of your machine).

I develop different projects on different machines, so when one machine goes down I have to set up the project on the other machine. Sometimes this is easy… sometimes not. One project quickly moved over (aside from some productivity hits), one project moved over except for talking to some hardware (which I just didn’t bother to set up), and one project I couldn’t compile the support libraries for ( first wxWidgets compile/link oddities, then wxWebKit link errors. Big C++ libraries are quite a pain sometimes).

The iBook is back up and running, so progress can move forward at a slightly faster (and portable!) pace once again.

On the machine front, yesterday I spent a good chunk of time updating my Debian server to the latest of everything. That did mean the blogs and the wiki were flaky yesterday, but they’re back to normal today.

July 19, 2007

Gathering and Researching (or: The Art Of Putting Together an Estimate)

Filed under: Distribute — Ryan Wilcox @ 8:45 pm

In case you haven’t noticed, the work so far in Project Distribute has simply been gathering components: finding a solid HTML control, finding a HTML template system. Part of this gathering is testing and research: making sure that we can do everything with the control that we’ll need to do with the control.

We’ve done that homework for wxWebKit. I haven’t yet finished evaluating Google CTemplate, and as soon as I find more than a few hours at a time to do some testing, that’ll be done as well.

Over the last few nights I’ve been sitting down, thinking about what I know so far, refining the Milestones I gave you before, and putting an estimate on each milestone. This thinking has uncovered some more questions I need to investigate, and more places we need to gather components.

I have one technical question left to resolve before I have a solid estimate for the first Milestone. With that line-item done I can gather up the components needed for the next steps, start learning and evaluating them, and estimate the next steps.

I can’t give you an estimate until I figure out if we can find good components or if we have to create something big from scratch. That’s where the research comes in (and why I’ve been poking hard at wxWebKit: it plays a pretty important part in the puzzle, we have to make sure it’s a good choice)

July 16, 2007

Shifting Gears

Filed under: Distribute — Ryan Wilcox @ 11:00 am

We know a lot about the JavaScript to/from C++ API in WebKit. We don’t have all the pieces yet (we don’t know how to call C++ from JavaScript, for example, and the code to convert a C++ value into a KJS::JSValue needs to be written), but for the sake of research let’s keep going forward for now.

We have a web control, but we need something to display in it. In our case (constructing HTML mostly from scratch) we really don’t want to code all that HTML into our C++ program: that would make the HTML much harder to change than it should be. Ideally we want to load the HTML from a file, run it through some HTML template system that replaces (markers we left in the HTML code) with values from the C++ program.

Over the weekend I looked into these systems, and I found two: ClearSilver and Google CTemplate. ClearSilver is a C based library, available for multiple languages (including C++ bindings). It looks well used from what I can see. It also looks really complex, and the C++ bindings are very involved.

Google’s CTemplate is much better: a very simple C++ facing API, and still a fairly powerful HTML templating language. It seems that Google is switching from ClearSilver over to this. This says two things to me: that it’s Fast Enough For Google, and There Has To Be A Reason Google Wrote Its Own When It Had ClearSilver. (I suspect the answer is: this is simpler).

So I’m going to take Google’s CTemplate out for a ride: see if we can get a very basic version of our main screen working.

July 9, 2007

Getting/Setting JavaScript variables with WebKit

Filed under: Distribute — Ryan Wilcox @ 8:53 pm

Further progress on the WebKit front. I’m working with Kevin Ollivier, the author of wxWebKit, to put C++ to JavaScript utilities into the framework itself. Right now this is focused mostly on calling JavaScript functions from C++, and translating C++ values into JSValues, but I suspect other things will come from this as well.

For example, this weekend (along with some C++ interface structuring) I was looking at my JavaScript function calling code and found something. Specifically, how to get and set JavaScript variables from C++. Not exactly what I was looking for, but very useful none-the-less.

void PutJSValue(const std::string& memberName, KJS::JSValue* val)  
{
WebCore::KJSProxy* js = m_frame->scriptProxy();
KJS::ScriptInterpreter* interp = js->interpreter();
KJS::ExecState* exec = interp->globalExec();
//TODO: error checking ASSERT(!exec->hadException()); 
KJS::JSLock lock;
//look up the object 
KJS::JSValue* JSMemberName = KJS::jsString( memberName.c_str() );
KJS::Identifier identifier( JSMemberName->toString(exec) );
KJS::JSObject* rootObj = interp->globalObject();
rootObj->put(exec, identifier, val);
if ( exec->hadException() )
{
KJS::JSValue* theEx = exec->exception();
//TODO: figure out how to convert UString to a wxString 
//use JSValue.toString() gives us a UString 
throw RuntimeJSError("Error while setting value in JavaScript");
}
}

There are still some things to do, of course. First, the matter of capturing exception information from JavaScript. Secondly, I don’t know if this method supports setting object members: say window.document.baseURI. I can access window.something, but I don’t know about anything more complex. Right now I’m not going to worry about it.

Next up: converting C++ values into JavaScript values (and back). Someday I will get to calling C++ functions from JavaScript…

June 28, 2007

Calling JavaScript functions from C++ with WebKit (Part 2)

Filed under: Distribute — Ryan Wilcox @ 10:34 pm

I didn’t know there was going to be a part 2 to this little adventure: I thought Part 1 was enough. Well, as long as your program liked constructing JavaScript syntax by hand that is, which would work in a pinch.

Today I followed a rabbit hole and found a much better way to call a JavaScript function from C++/WebKit: one where we don’t have to manually build the calling line ourselves. So instead of having to build myFunction(param1, param2) we can just specify the function name to call, and the params to pass.

Below is the C++ function I wrote for the simplest case (calling a JavaScript function with no parameters).

void ClassListFrame::CallJSFunctionNameWithParams(const std::string& jsFunCall)
{
    //A C++ translation of [ WebScriptObject.mm callWebScriptMethod... ]
    
    WebCore::KJSProxy* js = frame->scriptProxy();
    KJS::ScriptInterpreter* interp = js->interpreter();
    KJS::ExecState* exec = interp->globalExec();
    //TODO: error checking ASSERT(!exec->hadException());
    
    KJS::JSLock lock;
    //look up the function object

    KJS::JSValue* functionName = KJS::jsString( jsFunCall.c_str() );    
    KJS::Identifier identifier( functionName->toString(exec) );
    KJS::JSObject* rootObj = interp->globalObject();    
    KJS::JSValue* functionObj = rootObj->get(exec, identifier);

//now that we've looked it up, make sure we have it (and it's a function) if ( !functionObj || !functionObj->isObject() ) throw RuntimeJSError("Requested Object could not Be Found!"); KJS::JSObject* funcImp = static_cast<KJS::JSObject*>( functionObj ); if ( !funcImp->implementsCall() ) throw RuntimeJSError("Retrieved JavaScript Object wasn't a function");
KJS::List paramLst; //TODO: allow parameters //KJS::JSValue* paramValue = ... (see kjs/value.h for jsString(), jsBoolean(), jsNumber() ) //and also JavaScriptCore/bindings/objc/objc_utility.mm for how Cocoa does the conversions //THEN: paramLst.append( paramValue ) //FINALLY! Call it! KJS::JSObject* thisObj = const_cast<KJS::JSObject*>(rootObj); KJS::JSValue* result = funcImp->call(exec, thisObj, paramLst); if (!result) throw RuntimeJSError("Nothing returned from JavaScript function"); }

(thank you CodeColorizer! Ugly non-CSS code, but hey…)

In the future we need to find a way to pass parameters through to JavaScript. One way would be to write several different versions of this function, each accepting 1 more parameter. The trick is to get the type-strict C++ to not care what types these C++ parameters are, instead only caring: “if we can convert it, it’s OK”. I see two ways of doing that right now: either with subclasses or with templates. I like the templates idea because then the caller doesn’t have to worry about creating “manually” translating their int into the appropriate subclass (a kind of encapsulation from what’s actually going on that I don’t think is doable with a subclass approach). Although beyond ease-of-calling (having my “user of the API” hat on) I don’t think I’ll see many benefits to the writer (having my “creator of the API” hat on) to using templates.

Next up to bat (really this time): Calling C++ functions from JavaScript.

June 19, 2007

Carbon “Death” (and my plans about it)

Filed under: Distribute,General Information,VonTrapp — Ryan Wilcox @ 10:13 pm

Given Apple’s 64 Bit Carbon plans, a lot of people are wondering if Carbon is dead (or not). (See one example of this over at Michael Tsai’s blog entry on 64-bit Carbon).

All I have to say here: there’s an advantage here that wxWidgets brings me: one can hope that the wxCocoa port of wxWidgets will step up to the plate. If it does (and I’ll probably pitch in here), then I just recompile my wxWidgets apps with wxCocoa, and I’ve made a major shift already. No rewriting all of my view (or controller) code, just recompile the underlaying library. Since wxWidgets is an abstraction, it just abstracted the API change away from me.

The only thing I would need to rework in my apps would be the places where I’ve called Carbon routines directly to provide Mac OS specific features, or where I’ve modified wxWidgets and now need to rewrite my modifications.

Calling JavaScript functions from C++ with WebKit

Filed under: Distribute — Ryan Wilcox @ 12:06 pm

I figured out how to call JavaScript functions from C++ with WebKit today.


WebCore::KJSProxy* js = frame->scriptProxy();
if (js)
{
js->evaluate(filename, linenum, scriptText, Node);
//filename can be "" and linenum can be 0 if you want to just execute the line in scriptText
}

Now the next task: calling C++ objects from JavaScript…

June 18, 2007

First Steps the Second: Calling into and out of JavaScript

Filed under: Distribute — Ryan Wilcox @ 11:40 pm

The reason I picked WebKit is that it (seemed) to be easy to call JavaScript from C++ and (hopefully) the reverse too (by some glue JavaScript magic).

I have a pointer on how to do the former (JS from C++). Getting this working is my next task.

June 17, 2007

Little First Steps

Filed under: Distribute — Ryan Wilcox @ 9:25 pm

In Project Distribute we want to display some HTML in a wxWidgets application. One such component is wxWebKit, and the one we’re trying out first. wxWebKit is mentioned as a WebKit project here. See also the wxWebKit sourceforge page

The last few weeks have been filled with build errors (both wxWebKit’s an my own), but tonight I finally got arbitrary HTML displaying! Now to write the code that generates that HTML string.