Refactored javascript shell implementation to support JSC and v8.
Also changed configuration in examples Makefile.in to allow switching modes. git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/branches/oliverb-javascript-v8@13783 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
parent
672208d8d2
commit
10dc758cad
6 changed files with 632 additions and 200 deletions
95
Tools/javascript/js_shell.cxx
Normal file
95
Tools/javascript/js_shell.cxx
Normal file
|
|
@ -0,0 +1,95 @@
|
|||
#include "js_shell.h"
|
||||
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#ifdef __GNUC__
|
||||
#include <dlfcn.h>
|
||||
#define LOAD_LIBRARY(name) dlopen(name, RTLD_LAZY)
|
||||
#define CLOSE_LIBRARY(handle) dlclose(handle)
|
||||
#define LIBRARY_ERROR dlerror
|
||||
#define LIBRARYFILE(name) std::string("lib").append(name).append(".so")
|
||||
#else
|
||||
#error "implement dll loading"
|
||||
#endif
|
||||
|
||||
JSShell::~JSShell() {
|
||||
|
||||
for(std::vector<HANDLE>::iterator it = loaded_modules.begin();
|
||||
it != loaded_modules.end(); ++it) {
|
||||
HANDLE handle = *it;
|
||||
CLOSE_LIBRARY(handle);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
bool JSShell::ImportModule(const std::string& name) {
|
||||
|
||||
std::string lib_name = LIBRARYFILE(name);
|
||||
|
||||
HANDLE handle = LOAD_LIBRARY(lib_name.c_str());
|
||||
if(handle == 0) {
|
||||
std::cout << "Could not load library " << lib_name << ":"
|
||||
<< std::endl << LIBRARY_ERROR() << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
if(!RegisterModule(handle, name)) {
|
||||
std::cout << "Could not find initializer function in " << lib_name << std::endl;
|
||||
CLOSE_LIBRARY(handle);
|
||||
return false;
|
||||
}
|
||||
|
||||
loaded_modules.push_back(handle);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool JSShell::RunScript(const std::string& scriptPath) {
|
||||
std::string source = ReadFile(scriptPath);
|
||||
if(!InitializeEngine()) return false;
|
||||
|
||||
if(!ExecuteScript(source, scriptPath)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return DisposeEngine();
|
||||
}
|
||||
|
||||
bool JSShell::RunShell() {
|
||||
|
||||
if(!InitializeEngine()) return false;
|
||||
|
||||
static const int kBufferSize = 1024;
|
||||
while (true) {
|
||||
char buffer[kBufferSize];
|
||||
printf("> ");
|
||||
char* str = fgets(buffer, kBufferSize, stdin);
|
||||
if (str == NULL) break;
|
||||
std::string source(str);
|
||||
ExecuteScript(source, "(shell)");
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
std::string JSShell::ReadFile(const std::string& fileName)
|
||||
{
|
||||
std::string script;
|
||||
|
||||
std::ifstream file(fileName.c_str());
|
||||
if (file.is_open()) {
|
||||
while ( file.good() ) {
|
||||
std::string line;
|
||||
getline(file, line);
|
||||
script.append(line);
|
||||
script.append("\n");
|
||||
}
|
||||
file.close();
|
||||
} else {
|
||||
std::cout << "Unable to open file " << fileName << "." << std::endl;
|
||||
}
|
||||
|
||||
return script;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue