<From a thread on linkedin>
Michael,
I did (ala
http://code.google.com/apis/v8/build.html)
$ svn checkout http://v8.googlecode.com/svn/trunk/ v8-read-only
$ cd v8-read-only
$ scons arch=x64
$ scons arch=x64 library=shared
$ g++ -c ocv8js.cpp -Iinclude -L. -o ocv8js.o -lpthread
$ cobc -x ocv8.cob ocv8js.o -lv8 -lstdc++ -L .
$ LD_LIBRARY_PATH=$PWD ./ocv8
with
ocv8js.cpp
#include <v8.h>
#include <string.h>
using namespace v8;
extern "C" {
int CBL_OC_V8JAVASCRIPT(char* arg, char* out, int outlen) {
// Create a stack-allocated handle scope.
HandleScope handle_scope;
// Create a new context.
Persistent<Context> context = Context::New();
// Enter the created context for compiling and
// running the hello world script.
Context::Scope context_scope(context);
// Create a string containing the JavaScript source code.
Handle<String> source = String::New(arg);
// Compile the source code.
Handle<Script> script = Script::Compile(source);
// Run the script to get the result.
Handle<Value> result = script->Run();
// Dispose the persistent context.
context.Dispose();
// Convert the result to an ASCII string and cpy to COBOL
String::AsciiValue ascii(result);
strncpy(out, *ascii, outlen);
return 0;
}
}
and
OCOBOL >>SOURCE FORMAT IS FIXED
*> ***************************************************************
*> Author: Brian Tiffin
*> Date: 20120227
*> Purpose: Embed V8 javascript
*>
*> Tectonics: from http://code.google.com/apis/v8/build.html
*> and in a fresh svn checkout directory
*> scons library=shared arch=x64
*> g++ -c ocv8js.cpp -Iinclude -L. -o ocv8js.o -lpthread
*> cobc -x ocv8.cob ocv8js.o -lv8 -lstdc++ -L .
*> ***************************************************************
identification division.
program-id. ocv8.
data division.
77 result usage binary-long.
01 v8-value pic x(32767).
*> -*********-*********-*********-*********-*********-*********-**
procedure division.
display "Calling CBL_OC_V8JAVASCRIPT" end-display
call "CBL_OC_V8JAVASCRIPT"
using
"'Hello' + ', world'" & x"00"
v8-value
by value function length(v8-value)
returning result
on exception
display "v8 linkage problem, result garbage" end-display
continue
end-call
move function substitute(v8-value, x"00", space) to v8-value
display "V8 gives :" function trim(v8-value) ":" end-display
goback.
end program ocv8.
and as none of this is installed, need to tweak the library lookup path for testing
$ LD_LIBRARY_PATH=$PWD ./ocv8
Calling CBL_OC_V8JAVASCRIPT
V8 gives :Hello, world:
Worked the beauty first try. But, man is the V8 code ever codey, err, I mean top-end C++.
v8 sources really are top-end everyone. But C++ is always codey lookin'. The more high end, the codey-er. Cheers,
Brian
and I lied, nothing ever works first try, but two in this case...I forgot the BY VALUE on the length int, and first try was a segfault. Always heart warming and very motivational when experimenting with unknown new libraries.