Add scripting-lib (link with v8 when it's available)

This commit is contained in:
David Capello 2012-09-02 17:35:14 -03:00
parent 871ba75e74
commit 810b082500
7 changed files with 198 additions and 4 deletions

View File

@ -11,7 +11,9 @@ if(MSVC)
endif(MSVC)
# Libraries in this directory
set(aseprite_libraries aseprite-library undo-lib filters-lib ui-lib she gfx-lib base-lib)
set(aseprite_libraries aseprite-library scripting-lib
undo-lib filters-lib ui-lib
she gfx-lib base-lib)
# Directories where .h files can be found
include_directories(. .. ../third_party)
@ -132,19 +134,24 @@ else()
add_definitions(-DALLEGRO4_WITH_RESIZE_PATCH)
endif()
# All libraries for .exe files
set(all_libs ${aseprite_libraries} ${libs3rdparty} ${sys_libs})
######################################################################
# ASEPRITE libraries
add_subdirectory(base)
add_subdirectory(filters)
add_subdirectory(gfx)
add_subdirectory(scripting)
add_subdirectory(she)
add_subdirectory(ui)
add_subdirectory(undo)
if(V8_FOUND)
list(APPEND libs3rdparty ${V8_LIBRARIES})
endif()
# All libraries for .exe files
set(all_libs ${aseprite_libraries} ${libs3rdparty} ${sys_libs})
add_library(aseprite-library
app.cpp
app_menus.cpp

View File

@ -24,6 +24,7 @@
#include "base/memory_dump.h"
#include "console.h"
#include "resource_finder.h"
#include "scripting/engine.h"
#include "she/she.h"
#include "ui/base.h"
@ -86,6 +87,7 @@ int app_main(int argc, char* argv[])
MemLeak memleak;
ui::GuiSystem guiSystem;
App app(argc, argv);
scripting::Engine scriptingEngine;
// Change the name of the memory dump file
{

View File

@ -0,0 +1,18 @@
# ASEPRITE
# Copyright (C) 2001-2012 David Capello
find_package(V8)
set(V8_FOUND ${V8_FOUND} PARENT_SCOPE)
set(V8_INCLUDE_DIR ${V8_INCLUDE_DIR} PARENT_SCOPE)
set(V8_LIBRARIES ${V8_LIBRARIES} PARENT_SCOPE)
if(V8_FOUND)
include_directories(${V8_INCLUDE_DIR})
add_definitions(-DHAVE_V8_LIBRARY)
else()
message(WARNING "V8 not found, compiling without JS scripting support")
endif()
add_library(scripting-lib
engine.cpp)

44
src/scripting/engine.cpp Normal file
View File

@ -0,0 +1,44 @@
/* ASEPRITE
* Copyright (C) 2001-2012 David Capello
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "scripting/engine.h"
#ifdef HAVE_V8_LIBRARY
#include "scripting/engine_v8.h"
#else
#include "scripting/engine_none.h"
#endif
namespace scripting {
Engine::Engine() : m_impl(new EngineImpl) {
}
Engine::~Engine() {
delete m_impl;
}
bool Engine::supportEval() const {
return m_impl->supportEval();
}
void Engine::eval(const std::string& script) {
m_impl->eval(script);
}
} // namespace scripting

41
src/scripting/engine.h Normal file
View File

@ -0,0 +1,41 @@
/* ASEPRITE
* Copyright (C) 2001-2012 David Capello
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifndef SCRIPTING_ENGINE_H_INCLUDED
#define SCRIPTING_ENGINE_H_INCLUDED
#include <string>
namespace scripting {
class Engine {
public:
Engine();
~Engine();
bool supportEval() const;
void eval(const std::string& script);
private:
class EngineImpl;
EngineImpl* m_impl;
};
}
#endif

View File

@ -0,0 +1,33 @@
/* ASEPRITE
* Copyright (C) 2001-2012 David Capello
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "scripting/engine.h"
class scripting::Engine::EngineImpl
{
public:
EngineImpl() { }
bool supportEval() const {
return false;
}
void eval(const std::string& scriptString) {
}
};

49
src/scripting/engine_v8.h Normal file
View File

@ -0,0 +1,49 @@
/* ASEPRITE
* Copyright (C) 2001-2012 David Capello
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "scripting/engine.h"
#include <v8.h>
using namespace v8;
class scripting::Engine::EngineImpl
{
public:
EngineImpl() { }
bool supportEval() const {
return true;
}
void eval(const std::string& scriptString) {
HandleScope handle_scope;
Persistent<Context> context = Context::New();
Context::Scope context_scope(context);
Handle<String> source = String::New(scriptString.c_str());
Handle<Script> script = Script::Compile(source);
Handle<Value> result = script->Run();
context.Dispose();
String::AsciiValue ascii(result);
printf("%s\n", *ascii);
}
};