Skip to content

Instantly share code, notes, and snippets.

@timglabisch
Last active December 29, 2015 05:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timglabisch/07d50cee84cebff74c40 to your computer and use it in GitHub Desktop.
Save timglabisch/07d50cee84cebff74c40 to your computer and use it in GitHub Desktop.
#include "bootstrap.h"
#include <limits.h>
#include "gtest/gtest.h"
#include <v8.h>
#include <iostream>
TEST(Foo, FooBasic1) {
EXPECT_EQ(1, 1);
EXPECT_EQ(1, 1);
EXPECT_GT(5, 0);
}
TEST(Foo, FooBasic2) {
EXPECT_EQ(1, 1);
EXPECT_EQ(1, 1);
EXPECT_GT(5, 0);
}
TEST(Foo, FooBasic3) {
EXPECT_EQ(1, 1);
EXPECT_EQ(1, 1);
EXPECT_GT(5, 0);
}
using namespace v8;
static void LogCallback(const v8::FunctionCallbackInfo<v8::Value>& args) {
std::cout << "???" << std::endl;
args.GetReturnValue().Set(
String::New("DAMN FUCKING SHIT!")
);
}
int bootstrap(int argc, char* argv[]) {
// Get the default Isolate created at startup.
Isolate* isolate = Isolate::GetCurrent();
// Create a stack-allocated handle scope.
HandleScope handle_scope(isolate);
// Create a new context.
Handle<Context> context = Context::New(isolate);
// Here's how you could create a Persistent handle to the context, if needed.
Persistent<Context> persistent_context(isolate, context);
// Enter the created context for compiling and
// running the hello world script.
Context::Scope context_scope(context);
Local<ObjectTemplate> o = ObjectTemplate::New();
o->Set(String::New("hello"), String::New("worldObject!"));
Local<FunctionTemplate> f = FunctionTemplate::New(LogCallback);
o->Set(String::New("sayHello"), f);
context->Global()->Set(String::New("hello"), String::New("world"));
context->Global()->Set(String::New("helloObj"), o->NewInstance());
// Create a string containing the JavaScript source code.
Handle<String> source = String::New("9999999999999999 + 'Hello' + ', World!' + hello + helloObj.hello + helloObj.sayHello()");
// Compile the source code.
Handle<Script> script = Script::Compile(source);
// Run the script to get the result.
Handle<Value> result = script->Run();
// The persistent handle needs to be eventually disposed.
persistent_context.Dispose();
// Convert the result to an ASCII string and print it.
String::AsciiValue ascii(result);
printf("%s\n", *ascii);
return 0;
}
GTEST_API_ int tests(int argc, char **argv) {
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
cmake_minimum_required (VERSION 2.6)
project (proj)
set(PROJ_NAME "proj")
set(PROJ_NAME_TEST "test")
set(INCLUDE_DIRECTORIES
/home/dev/proj/cpp/v8/include/
/home/dev/proj/cpp/gtest/include/
/usr/local/include
)
set(TARGET_LINK_LIBRARIES
/home/dev/proj/cpp/v8/out/x64.release/obj.target/tools/gyp/libv8_base.x64.a
/home/dev/proj/cpp/v8/out/x64.release/obj.target/tools/gyp/libv8_snapshot.a
/home/dev/proj/cpp/v8/out/x64.release/obj.target/third_party/icu/libicui18n.a
/home/dev/proj/cpp/v8/out/x64.release/obj.target/third_party/icu/libicuuc.a
/home/dev/proj/cpp/v8/out/x64.release/obj.target/third_party/icu/libicudata.a
/home/dev/proj/cpp/gtest/lib/.libs/libgtest_main.a
/home/dev/proj/cpp/gtest/lib/.libs/libgtest.a
pthread
)
FILE(GLOB FILES_CPP "*.cpp")
add_executable (${PROJ_NAME}
${FILES_CPP}
main.cc
)
add_executable (${PROJ_NAME_TEST}
${FILES_CPP}
main_test.cc
)
include_directories (
${INCLUDE_DIRECTORIES}
)
TARGET_LINK_LIBRARIES(${PROJ_NAME} ${TARGET_LINK_LIBRARIES})
TARGET_LINK_LIBRARIES(${PROJ_NAME_TEST} ${TARGET_LINK_LIBRARIES})
dev@ubuntu:~/proj/cpp/gtestdemo$ gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/4.7/lto-wrapper
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu/Linaro 4.7.3-1ubuntu1' --with-bugurl=file:///usr/share/doc/gcc-4.7/README.Bugs --enable-languages=c,c++,go,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.7 --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.7 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-gnu-unique-object --enable-plugin --with-system-zlib --enable-objc-gc --with-cloog --enable-cloog-backend=ppl --disable-cloog-version-check --disable-ppl-version-check --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 4.7.3 (Ubuntu/Linaro 4.7.3-1ubuntu1)
dev@ubuntu:~/proj/cpp/gtestdemo$ cmake . && make && ./proj
-- Configuring done
-- Generating done
-- Build files have been written to: /home/dev/proj/cpp/gtestdemo
[ 16%] Building CXX object CMakeFiles/proj.dir/request.cpp.o
/home/dev/proj/cpp/gtestdemo/request.cpp:5:30: error: reference to ‘FunctionCallbackInfo’ is ambiguous
In file included from /home/dev/proj/cpp/gtestdemo/request.h:1:0,
from /home/dev/proj/cpp/gtestdemo/request.cpp:1:
/home/dev/proj/cpp/v8/include/v8.h:322:34: error: candidates are: template<class F> class v8::FunctionCallbackInfo
/home/dev/proj/cpp/v8/include/v8.h:2383:7: error: template<class T> class v8::FunctionCallbackInfo
/home/dev/proj/cpp/gtestdemo/request.cpp:5:30: error: ‘FunctionCallbackInfo’ does not name a type
/home/dev/proj/cpp/gtestdemo/request.cpp:5:30: error: ISO C++ forbids declaration of ‘parameter’ with no type [-fpermissive]
/home/dev/proj/cpp/gtestdemo/request.cpp:5:50: error: expected ‘,’ or ‘...’ before ‘<’ token
/home/dev/proj/cpp/gtestdemo/request.cpp:5:6: error: prototype for ‘void request::getParam(int)’ does not match any in class ‘request’
In file included from /home/dev/proj/cpp/gtestdemo/request.cpp:1:0:
/home/dev/proj/cpp/gtestdemo/request.h:11:17: error: candidate is: static void request::getParam(const v8::FunctionCallbackInfo<v8::Value>&)
/home/dev/proj/cpp/gtestdemo/request.cpp:9:42: error: reference to ‘FunctionCallbackInfo’ is ambiguous
In file included from /home/dev/proj/cpp/gtestdemo/request.h:1:0,
from /home/dev/proj/cpp/gtestdemo/request.cpp:1:
/home/dev/proj/cpp/v8/include/v8.h:322:34: error: candidates are: template<class F> class v8::FunctionCallbackInfo
/home/dev/proj/cpp/v8/include/v8.h:2383:7: error: template<class T> class v8::FunctionCallbackInfo
/home/dev/proj/cpp/gtestdemo/request.cpp:9:42: error: ‘FunctionCallbackInfo’ does not name a type
/home/dev/proj/cpp/gtestdemo/request.cpp:9:42: error: ISO C++ forbids declaration of ‘parameter’ with no type [-fpermissive]
/home/dev/proj/cpp/gtestdemo/request.cpp:9:62: error: expected ‘,’ or ‘...’ before ‘<’ token
/home/dev/proj/cpp/gtestdemo/request.cpp:9:15: error: prototype for ‘v8::Local<v8::Object> request::newInstance(int)’ does not match any in class ‘request’
In file included from /home/dev/proj/cpp/gtestdemo/request.cpp:1:0:
/home/dev/proj/cpp/gtestdemo/request.h:12:34: error: candidate is: static v8::Local<v8::Object> request::newInstance(const v8::FunctionCallbackInfo<v8::Value>&)
make[2]: *** [CMakeFiles/proj.dir/request.cpp.o] Error 1
make[1]: *** [CMakeFiles/proj.dir/all] Error 2
#include "request.h"
using namespace v8;
void request::getParam(const FunctionCallbackInfo<Value>& args) {
args.GetReturnValue().Set(String::New("awesome"));
}
Local<Object> request::newInstance(const FunctionCallbackInfo<Value>& args) {
//Isolate* isolate = Isolate::GetCurrent();
//HandleScope handle_scope(isolate);
Local<ObjectTemplate> o = ObjectTemplate::New();
Local<FunctionTemplate> f = FunctionTemplate::New(getParam);
o->Set(String::New("getParam"), f);
Local<Object> inst = o->NewInstance();
request r = new request;
inst->SetInternalField(0, r);
return inst;
}
#include <v8.h>
#include <map>
#ifndef REQUEST_H
#define REQUEST_H
class request {
// v8::String filename;
// std::map<v8::String, v8::String> params;
public:
static void getParam(const v8::FunctionCallbackInfo<v8::Value>& args);
static v8::Local<v8::Object> newInstance(const v8::FunctionCallbackInfo<v8::Value>& args);
};
#endif
private:
friend class Utils;
template<class F, class M> friend class Persistent;
template<class F> friend class Local;
template<class F> friend class FunctionCallbackInfo; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
template<class F> friend class PropertyCallbackInfo;
template<class F> friend class internal::CustomArguments;
friend Handle<Primitive> Undefined(Isolate* isolate);
friend Handle<Primitive> Null(Isolate* isolate);
friend Handle<Boolean> True(Isolate* isolate);
friend Handle<Boolean> False(Isolate* isolate);
friend class Context;
friend class HandleScope;
.....
/**
* The argument information given to function call callbacks. This
* class provides access to information about the context of the call,
* including the receiver, the number and values of arguments, and
* the holder of the function.
*/
template<typename T>
class FunctionCallbackInfo { <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
public:
V8_INLINE int Length() const;
V8_INLINE Local<Value> operator[](int i) const;
V8_INLINE Local<Function> Callee() const;
V8_INLINE Local<Object> This() const;
V8_INLINE Local<Object> Holder() const;
V8_INLINE bool IsConstructCall() const;
V8_INLINE Local<Value> Data() const;
V8_INLINE Isolate* GetIsolate() const;
V8_INLINE ReturnValue<T> GetReturnValue() const;
// This shouldn't be public, but the arm compiler needs it.
static const int kArgsLength = 7;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment