publicenum ElementType { TYPE, //If you want to annotate class, interface, enum.. FIELD, //If you want to annotate field (includes enum constants) METHOD, //If you want to annotate method PARAMETER, //If you want to annotate parameter CONSTRUCTOR, //If you want to annotate constructor LOCAL_VARIABLE, //.. ANNOTATION_TYPE, //.. PACKAGE, //.. TYPE_PARAMETER, //..(java 8) TYPE_USE; //..(java 8)
Local<Script> script; if (!maybe_script.ToLocal(&script)) { // Print errors that happened during compilation. if (report_exceptions) ReportException(isolate, &try_catch); returnfalse; }
// 部分代码略去 maybe_result = script->Run(realm); if (options.code_cache_options == ShellOptions::CodeCacheOptions::kProduceCacheAfterExecute) { // Serialize and store it in memory for the next execution. ScriptCompiler::CachedData* cached_data = ScriptCompiler::CreateCodeCache(script->GetUnboundScript()); StoreInCodeCache(isolate, source, cached_data); delete cached_data; } if (process_message_queue && !EmptyMessageQueues(isolate)) success = false; // 关键 data->realm_current_ = data->realm_switch_; } Local<Value> result; if (!maybe_result.ToLocal(&result)) { DCHECK(try_catch.HasCaught()); // Print errors that happened during execution. if (report_exceptions) ReportException(isolate, &try_catch); returnfalse; } DCHECK(!try_catch.HasCaught()); if (print_result) { if (options.test_shell) { if (!result->IsUndefined()) { // If all went well and the result wasn't undefined then print // the returned value. v8::String::Utf8Value str(isolate, result); fwrite(*str, sizeof(**str), str.length(), stdout); printf("\n"); } } else { v8::String::Utf8Value str(isolate, Stringify(isolate, result)); fwrite(*str, sizeof(**str), str.length(), stdout); printf("\n"); } } return success; }
看着就跟官方 demo 里面执行 hello world 差不多, 但是其中的一行有点令人在意.
1
if (process_message_queue && !EmptyMessageQueues(isolate)) success = false;
/** * Pumps the message loop for the given isolate. * * The caller has to make sure that this is called from the right thread. * Returns true if a task was executed, and false otherwise. Unless requested * through the |behavior| parameter, this call does not block if no task is * pending. The |platform| has to be created using |NewDefaultPlatform|. */ V8_PLATFORM_EXPORT boolPumpMessageLoop( v8::Platform* platform, v8::Isolate* isolate, MessageLoopBehavior behavior = MessageLoopBehavior::kDoNotWait);
v8::Local<v8::Script> script = v8::Script::Compile(ctx, source).ToLocalChecked(); v8::Local<v8::Value> result; if (!script->Run(ctx).ToLocal(&result)) { ReportException(isolate, &try_catch); // report exception, ignore the implementation here return; } // Convert the result to an UTF8 string and print it. v8::String::Utf8Value utf8(isolate, result); __android_log_print(ANDROID_LOG_INFO, "V8Native", "%s\n", *utf8);
Aside: When you compile C/C++ normally, you link with the system to provide implementations of the standard library methods your code uses.
JavaScript doesn't have these methods—either not with the same signatures or names (e.g, Math.atan in JavaScript vs atan in C), or because it's conceptually different (think malloc vs JavaScript's objects and garbage collection)—so Emscripten has to provide them for you.
The -Wl,xxx option for gcc passes a comma-separated list of tokens as a space-separated list of arguments to the linker. So
1
gcc -Wl,aaa,bbb,ccc
eventually becomes a linker call
1
ld aaa bbb ccc
target_link_libraries
The idea is that you build modules in CMake, and link them together. Let’s ignore header files for now, as they can be all included in your source files.
Say you have file1.cpp, file2.cpp, main.cpp. You add them to your project with:
1 2 3 4
ADD_LIBRARY(LibsModule file1.cpp file2.cpp )
Now you added them to a module called LibsModule. Keep that in mind. Say you want to link to pthread for example that’s already in the system. You can combine it with LibsModule using the command:
1
target_link_libraries(LibsModule -lpthread)
And if you want to link a static library to that too, you do this:
1
target_link_libraries(LibsModule liblapack.a)
And if you want to add a directory where any of these libraries are located, you do this:
What I see in your CMake file is a lot of redundancy. For example, why do you have texture_mapping, which is an executable module in your include directories? So you need to clean this up and follow the simple logic I explained. Hopefully it works.
In summary, it looks like this:
1 2 3 4 5 6 7 8 9 10 11 12 13
project (MyProgramExecBlaBla) #not sure whether this should be the same name of the executable, but I always see that "convention" cmake_minimum_required(VERSION 2.8)
The most important thing to understand is the module structure, where you create modules and link them all together with your executable. Once this works, you can complicate your project further with more details. Good luck!