[英]nested #if macro processing using java
我創建了一個代碼來處理這個文件
https://raw.githubusercontent.com/emscripten-core/emscripten/master/src/library_html5.js
逐行掃描# #f
、 #else
和#endif
達到特定級別后它可以正常工作,之后它會跳過一組行
這是我的代碼
static ScriptEngineManager mgr = new ScriptEngineManager();
static ScriptEngine engine = mgr.getEngineByName("JavaScript");
public static void main(String[] args) throws Exception {
Bindings bindings = engine.createBindings();
bindings.put("TARGET_NOT_SUPPORTED", true);
bindings.put("HTML5_SUPPORT_DEFERRING_USER_SENSITIVE_REQUESTS", true);
bindings.put("MINIMAL_RUNTIME", false);
bindings.put("EXIT_RUNTIME", false);
bindings.put("MIN_IE_VERSION", "TARGET_NOT_SUPPORTED");
bindings.put("USE_PTHREADS", true);
bindings.put("WASM_BACKEND", true);
bindings.put("MIN_FIREFOX_VERSION", 64);
bindings.put("MIN_SAFARI_VERSION", "TARGET_NOT_SUPPORTED");
bindings.put("ENVIRONMENT_MAY_BE_WORKER", false);
bindings.put("ENVIRONMENT_MAY_BE_NODE", false);
bindings.put("ENVIRONMENT_MAY_BE_SHELL", false);
bindings.put("DISABLE_DEPRECATED_FIND_EVENT_TARGET_BEHAVIOR", true);
bindings.put("OFFSCREENCANVAS_SUPPORT", true);
bindings.put("ASSERTIONS", false);
bindings.put("MIN_CHROME_VERSION", 37);
bindings.put("MIN_EDGE_VERSION", 13);
bindings.put("GL_DEBUG", false);
bindings.put("OFFSCREEN_FRAMEBUFFER", true);
bindings.put("TRACE_WEBGL_CALLS", true);
bindings.put("GL_SUPPORT_AUTOMATIC_ENABLE_EXTENSIONS", true);
bindings.put("GL_SUPPORT_EXPLICIT_SWAP_CONTROL", true);
bindings.put("MIN_WEBGL_VERSION", 1);
bindings.put("USE_WEBGPU", true);
bindings.put("WASM", true);
bindings.put("GL_EXTENSIONS_IN_PREFIXED_FORMAT", true);
bindings.put("MAX_WEBGL_VERSION", 3);
bindings.put("MIN_WEBGL_VERSION", 1);
bindings.put("FULL_ES2", true);
bindings.put("GL_ASSERTIONS", false);
bindings.put("GL_TRACK_ERRORS", true);
bindings.put("GL_POOL_TEMP_BUFFERS", true);
bindings.put("LEGACY_GL_EMULATION", false);
bindings.put("GL_FFP_ONLY", false);
bindings.put("GL_TESTING", false);
bindings.put("GL_PREINITIALIZED_CONTEXT", false);
bindings.put("GL_DISABLE_HALF_FLOAT_EXTENSION_IF_BROKEN", true);
bindings.put("OFFSCREEN_FRAMEBUFFER_FORBID_VAO_PATH", false);
bindings.put("WORKAROUND_OLD_WEBGL_UNIFORM_UPLOAD_IGNORED_OFFSET_BUG", true);
bindings.put("GL_EMULATE_GLES_VERSION_STRING_FORMAT", true);
bindings.put("WEBGL2_BACKWARDS_COMPATIBILITY_EMULATION", true);
bindings.put("PTHREAD_POOL_SIZE", 2);
bindings.put("PTHREADS_PROFILING", false);
bindings.put("USE_CLOSURE_COMPILER", true);
bindings.put("LOAD_SOURCE_MAP", false);
bindings.put("USE_OFFSET_CONVERTER", false);
bindings.put("PTHREADS_DEBUG", false);
bindings.put("OFFSCREENCANVASES_TO_PTHREAD", true);
bindings.put("ALLOW_BLOCKING_ON_MAIN_THREAD", false);
bindings.put("STACK_OVERFLOW_CHECK", false);
bindings.put("FULL_ES3", true);
bindings.put("library_webgl.js", true);
engine.setBindings(bindings, ScriptContext.GLOBAL_SCOPE);
engine.eval("var LibraryManager = {\"has\":function(){return true;}}");
Scanner s = new Scanner(new File("C:\\emsdk-1.39.13\\upstream\\emscripten\\src\\library_html5.js"));
Integer iflevel = 0;
Boolean flags[] = new Boolean[] {true,false,false,false,false};
Integer ln = 0;
while(s.hasNextLine()) {
String line = s.nextLine();
if(line.startsWith("#if")) {
if(line.indexOf("//")!=-1)
line = line.substring(0, line.indexOf("//"));
line = line.substring(4);
System.out.println(ln.toString() + " " + iflevel.toString() + " #if " + line.toString() + " " + engine.eval(line).toString());
if(engine.eval(line)!=null && engine.eval(line).toString().equals("true")) {
iflevel++;
flags[iflevel] = true;
}else if(engine.eval(line)!=null && engine.eval(line).toString().equals("false")) {
iflevel++;
flags[iflevel] = false;
}
}
else if(line.startsWith("#else")) {
if(iflevel>=0)
flags[iflevel] = !flags[iflevel];
System.out.println(ln.toString() + " " + iflevel.toString() + " " + line.toString() + " ");
}
else if(line.startsWith("#endif")) {
if(iflevel>0){
flags[iflevel] = false;
iflevel--;
}
if(iflevel<=0) {
iflevel = 0;
flags = new Boolean[] {true,false,false,false,false};
}
System.out.println(ln.toString() + " " + iflevel.toString() + " " + line.toString() + " ");
}
else if(flags[iflevel]==true) {
while(line.indexOf("{{{")!=-1) {
line = line.replace("{{{", "");
}
while(line.indexOf("}}}")!=-1) {
line = line.replace("}}}", "");
}
System.out.println(line);
if(iflevel<=0) {
iflevel = 0;
flags = new Boolean[] {true,false,false,false,false};
}
}else {
//println("level " + iflevel + " " + flags[iflevel] + " " + line);
}
ln++;
}
s.close();
}
問題從第2891
行開始,結果代碼變為錯誤的 JavaScript 由於缺少 function 聲明或打開 curl 括號並導致這種代碼
},
if (canvas.GLctxObject) GL.resizeOffscreenFramebuffer(canvas.GLctxObject);
似乎是標志被更改或 iflevel 錯誤
任何幫助都會很棒! - output 應該是已處理的 JavaScript 文件,所有條件均有效且宏已正確刪除
Java 的原因是這實際上是 Spring 引導中的@RequestMapping("/js/**")
我去了車把,原因是{{#if}}
和{{else}}
和{{/if}}
已在該庫中處理
我介紹了一個名為ifCond
的助手,它將調用 nashron
然后我將#if
替換為{{#ifCond
,將#else
替換為{{else}}
,將#endif
替換為{{/if}}
static ScriptEngineManager mgr = new ScriptEngineManager();
static ScriptEngine engine = mgr.getEngineByName("JavaScript");
public static void main(String[] args) throws Exception {
Bindings bindings = engine.createBindings();
bindings.put("TARGET_NOT_SUPPORTED", true);
bindings.put("HTML5_SUPPORT_DEFERRING_USER_SENSITIVE_REQUESTS", true);
bindings.put("MINIMAL_RUNTIME", false);
bindings.put("EXIT_RUNTIME", false);
bindings.put("MIN_IE_VERSION", "TARGET_NOT_SUPPORTED");
bindings.put("USE_PTHREADS", true);
bindings.put("WASM_BACKEND", true);
bindings.put("MIN_FIREFOX_VERSION", 64);
bindings.put("MIN_SAFARI_VERSION", "TARGET_NOT_SUPPORTED");
bindings.put("ENVIRONMENT_MAY_BE_WORKER", false);
bindings.put("ENVIRONMENT_MAY_BE_NODE", false);
bindings.put("ENVIRONMENT_MAY_BE_SHELL", false);
bindings.put("DISABLE_DEPRECATED_FIND_EVENT_TARGET_BEHAVIOR", true);
bindings.put("OFFSCREENCANVAS_SUPPORT", true);
bindings.put("ASSERTIONS", false);
bindings.put("MIN_CHROME_VERSION", 37);
bindings.put("MIN_EDGE_VERSION", 13);
bindings.put("GL_DEBUG", false);
bindings.put("OFFSCREEN_FRAMEBUFFER", true);
bindings.put("TRACE_WEBGL_CALLS", true);
bindings.put("GL_SUPPORT_AUTOMATIC_ENABLE_EXTENSIONS", true);
bindings.put("GL_SUPPORT_EXPLICIT_SWAP_CONTROL", true);
bindings.put("MIN_WEBGL_VERSION", 1);
bindings.put("USE_WEBGPU", true);
bindings.put("WASM", true);
bindings.put("GL_EXTENSIONS_IN_PREFIXED_FORMAT", true);
bindings.put("MAX_WEBGL_VERSION", 3);
bindings.put("MIN_WEBGL_VERSION", 1);
bindings.put("FULL_ES2", true);
bindings.put("GL_ASSERTIONS", false);
bindings.put("GL_TRACK_ERRORS", true);
bindings.put("GL_POOL_TEMP_BUFFERS", true);
bindings.put("LEGACY_GL_EMULATION", false);
bindings.put("GL_FFP_ONLY", false);
bindings.put("GL_TESTING", false);
bindings.put("GL_PREINITIALIZED_CONTEXT", false);
bindings.put("GL_DISABLE_HALF_FLOAT_EXTENSION_IF_BROKEN", true);
bindings.put("OFFSCREEN_FRAMEBUFFER_FORBID_VAO_PATH", false);
bindings.put("WORKAROUND_OLD_WEBGL_UNIFORM_UPLOAD_IGNORED_OFFSET_BUG", true);
bindings.put("GL_EMULATE_GLES_VERSION_STRING_FORMAT", true);
bindings.put("WEBGL2_BACKWARDS_COMPATIBILITY_EMULATION", true);
bindings.put("PTHREAD_POOL_SIZE", 2);
bindings.put("PTHREADS_PROFILING", false);
bindings.put("USE_CLOSURE_COMPILER", true);
bindings.put("LOAD_SOURCE_MAP", false);
bindings.put("USE_OFFSET_CONVERTER", false);
bindings.put("PTHREADS_DEBUG", false);
bindings.put("OFFSCREENCANVASES_TO_PTHREAD", true);
bindings.put("ALLOW_BLOCKING_ON_MAIN_THREAD", false);
bindings.put("STACK_OVERFLOW_CHECK", false);
bindings.put("ALLOW_MEMORY_GROWTH", false);
bindings.put("MAXIMUM_MEMORY", -1);
bindings.put("ABORTING_MALLOC", 0);
bindings.put("TEST_MEMORY_GROWTH_FAILS", false);
bindings.put("MEMORYPROFILER", false);
bindings.put("CAN_ADDRESS_2GB", false);
bindings.put("EMSCRIPTEN_TRACING", false);
bindings.put("MEMORY_GROWTH_LINEAR_STEP", -1);
bindings.put("MEMORY_GROWTH_GEOMETRIC_CAP", 96);
bindings.put("FAST_UNROLLED_MEMCPY_AND_MEMSET", false);
bindings.put("DECLARE_ASM_MODULE_EXPORTS", false);
bindings.put("EMULATE_FUNCTION_POINTER_CASTS", false);
bindings.put("SUPPORT_LONGJMP", false);
bindings.put("SUPPORT_ERRNO", false);
bindings.put("PROXY_POSIX_SOCKETS", 0);
bindings.put("SOCKET_WEBRTC", false);
bindings.put("ENVIRONMENT_MAY_BE_WEB", true);
bindings.put("MAIN_MODULE", 0);
bindings.put("USE_ASAN", false);
bindings.put("FULL_ES3", true);
bindings.put("library_webgl.js", true);
engine.setBindings(bindings, ScriptContext.GLOBAL_SCOPE);
engine.eval("var LibraryManager = {\"has\":function(){return true;}}");
String json = "{\"name\": \"world\"}";
JsonNode jsonNode = new ObjectMapper().readValue(json, JsonNode.class);
Handlebars handlebars = new Handlebars();
handlebars.registerHelper("json", Jackson2Helper.INSTANCE);
Context context = Context
.newBuilder(jsonNode)
.resolver(JsonNodeValueResolver.INSTANCE,
JavaBeanValueResolver.INSTANCE,
FieldValueResolver.INSTANCE,
MapValueResolver.INSTANCE,
MethodValueResolver.INSTANCE
)
.build();
handlebars.registerHelper("ifCond", new Helper<String>() {
public Object apply(String context, Options options) throws IOException {
try {
return engine.eval(context)!=null && engine.eval(context).toString().equals("true") ? options.fn(this) : options.inverse(this);
} catch (Exception e) {
System.err.println("Unable to process: " + context);
return options.inverse(this);
}
}
});
Scanner s = new Scanner(new File("C:\\emsdk-1.39.13\\upstream\\emscripten\\src\\library.js"));
StringBuffer sb = new StringBuffer();
while(s.hasNextLine()) {
String line = s.nextLine();
if(line.startsWith("#if")) {
if(line.indexOf("//")!=-1)
line = line.substring(0, line.indexOf("//"));
line = line.substring(4);
sb.append("{{#ifCond \"" + line + "\"}}\r\n");
}else if(line.startsWith("#else")) {
if(line.indexOf("//")!=-1)
line = line.substring(0, line.indexOf("//"));
sb.append("{{" + line.substring(1) + "}}\r\n");
}else if(line.startsWith("#endif")) {
if(line.indexOf("//")!=-1)
line = line.substring(0, line.indexOf("//"));
sb.append("{{/ifCond}}\r\n");
}else {
while(line.indexOf("{{{")!=-1) {
line = line.replace("{{{", "");
}
while(line.indexOf("}}}")!=-1) {
line = line.replace("}}}", "");
}
sb.append(line + "\r\n");
}
}
s.close();
Template template = handlebars.compileInline(sb.toString());
System.out.println(template.apply(context));
現在解決了 if 分支問題
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.