Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Delta Between Two Patch Sets: compiled/bindings.ipp

Issue 29398669: Issue 5063 - [emscripten] Make FilterNotifier calls more efficient (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore
Left Patch Set: Rebased and made the code deal with 64-bit properties Created April 7, 2017, 7:16 a.m.
Right Patch Set: Addressed comments Created April 20, 2017, 8 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « compiled/bindings.cpp ('k') | compiled/filter/ActiveFilter.h » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 /* 1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>, 2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-2017 eyeo GmbH 3 * Copyright (C) 2006-2017 eyeo GmbH
4 * 4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify 5 * Adblock Plus is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as 6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
8 * 8 *
9 * Adblock Plus is distributed in the hope that it will be useful, 9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
(...skipping 27 matching lines...) Expand all
38 { 38 {
39 typedef void* TYPEID; 39 typedef void* TYPEID;
40 40
41 enum class TypeCategory 41 enum class TypeCategory
42 { 42 {
43 UNKNOWN, 43 UNKNOWN,
44 VOID, 44 VOID,
45 INT, 45 INT,
46 INT64, 46 INT64,
47 FLOAT, 47 FLOAT,
48 DOUBLE,
48 DEPENDENT_STRING, 49 DEPENDENT_STRING,
49 OWNED_STRING, 50 OWNED_STRING,
50 STRING_REF, 51 STRING_REF,
51 CLASS_PTR 52 CLASS_PTR
52 }; 53 };
53 54
54 template<typename T> 55 template<typename T>
55 struct TypeInfo 56 struct TypeInfo
56 { 57 {
57 /* 58 /*
(...skipping 14 matching lines...) Expand all
72 { 73 {
73 if (std::is_void<T>()) 74 if (std::is_void<T>())
74 return TypeCategory::VOID; 75 return TypeCategory::VOID;
75 76
76 if (std::is_same<T, uint64_t>()) 77 if (std::is_same<T, uint64_t>())
77 return TypeCategory::INT64; 78 return TypeCategory::INT64;
78 79
79 if (std::is_integral<T>() || std::is_enum<T>()) 80 if (std::is_integral<T>() || std::is_enum<T>())
80 return TypeCategory::INT; 81 return TypeCategory::INT;
81 82
82 if (std::is_floating_point<T>()) 83 if (std::is_same<T, float>())
83 return TypeCategory::FLOAT; 84 return TypeCategory::FLOAT;
85
86 if (std::is_same<T, double>())
87 return TypeCategory::DOUBLE;
84 88
85 if (std::is_same<DependentString, T>() || std::is_same<const DependentStri ng, T>()) 89 if (std::is_same<DependentString, T>() || std::is_same<const DependentStri ng, T>())
86 return TypeCategory::DEPENDENT_STRING; 90 return TypeCategory::DEPENDENT_STRING;
87 91
88 if (std::is_same<OwnedString, T>() || std::is_same<const OwnedString, T>() ) 92 if (std::is_same<OwnedString, T>() || std::is_same<const OwnedString, T>() )
89 return TypeCategory::OWNED_STRING; 93 return TypeCategory::OWNED_STRING;
90 94
91 if (std::is_same<String&, T>() || std::is_same<const String&, T>() || 95 if (std::is_same<String&, T>() || std::is_same<const String&, T>() ||
92 std::is_same<DependentString&, T>()) 96 std::is_same<DependentString&, T>())
93 { 97 {
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
134 instance_function(instance_function) 138 instance_function(instance_function)
135 { 139 {
136 name[0] = '\0'; 140 name[0] = '\0';
137 141
138 // The function parameter is a pointer to the function pointer. 142 // The function parameter is a pointer to the function pointer.
139 // Emscripten's "function pointers" are actually integers indicating the 143 // Emscripten's "function pointers" are actually integers indicating the
140 // position in the call table. 0 represents nullptr. 144 // position in the call table. 0 represents nullptr.
141 if (!*reinterpret_cast<int*>(function)) 145 if (!*reinterpret_cast<int*>(function))
142 return; 146 return;
143 147
144 char signature[256]; 148 std::string signature;
145 int pos = 0; 149
146 if (returnType == TypeCategory::DEPENDENT_STRING || 150 // Add return type to the signature. Similar logic in Emscripten:
147 returnType == TypeCategory::OWNED_STRING) 151 // https://github.com/kripken/emscripten/blob/1.37.3/src/modules.js#L46
148 { 152 switch (returnType)
149 // Objects aren't really returned but passed as parameter. Note that 153 {
150 // this pointer might come before it but we don't care because both 154 case TypeCategory::DEPENDENT_STRING:
151 // are integers (pointers) as far as Emscripten is concerned. 155 case TypeCategory::OWNED_STRING:
152 signature[pos++] = 'v'; 156 // Technically, objects aren't really returned with clang. The caller
153 signature[pos++] = 'i'; 157 // instead adds the reference to the resulting object as an implicit
154 } 158 // parameter.
155 else if (returnType == TypeCategory::VOID) 159 signature += "vi";
156 signature[pos++] = 'v'; 160 break;
157 else if (returnType == TypeCategory::FLOAT) 161 case TypeCategory::VOID:
158 signature[pos++] = 'd'; 162 signature += 'v';
159 else if (returnType == TypeCategory::INT || 163 break;
160 returnType == TypeCategory::INT64 || 164 case TypeCategory::FLOAT:
161 returnType == TypeCategory::STRING_REF || 165 signature += 'f';
162 returnType == TypeCategory::CLASS_PTR) 166 break;
163 { 167 case TypeCategory::DOUBLE:
164 signature[pos++] = 'i'; 168 signature += 'd';
165 } 169 break;
166 else 170 case TypeCategory::INT:
167 throw std::runtime_error("Unexpected function return type"); 171 case TypeCategory::INT64:
168 172 case TypeCategory::STRING_REF:
173 case TypeCategory::CLASS_PTR:
174 signature += 'i';
175 break;
176 default:
177 throw std::runtime_error("Unexpected function return type");
178 }
179
180 // `this` pointer is an implicit parameter with clang and should be added
181 // to the signature.
169 if (instance_function) 182 if (instance_function)
170 { 183 signature += 'i';
171 // this pointer is an implicit parameter 184
172 signature[pos++] = 'i'; 185 // Add explicit parameters to the signature, Similar logic in Emscripten:
173 } 186 // https://github.com/kripken/emscripten/blob/1.37.3/src/modules.js#L67
174 187 for (const auto& type : argTypes)
175 for (const auto& item : argTypes) 188 {
176 { 189 switch (type)
177 if (item == TypeCategory::INT || item == TypeCategory::STRING_REF ||
178 item == TypeCategory::CLASS_PTR)
179 { 190 {
180 signature[pos++] = 'i'; 191 case TypeCategory::INT:
192 case TypeCategory::STRING_REF:
193 case TypeCategory::CLASS_PTR:
194 signature += 'i';
195 break;
196 case TypeCategory::INT64:
197 // See https://github.com/kripken/emscripten/blob/1.37.3/src/modules .js#L73,
198 // numerical types larger than 32-bit are split into multiple
199 // 32-bit parameters.
200 signature += "ii";
201 break;
202 case TypeCategory::FLOAT:
203 signature += 'f';
204 break;
205 case TypeCategory::DOUBLE:
206 signature += 'd';
207 break;
208 default:
209 throw std::runtime_error("Unexpected function argument type");
181 } 210 }
182 else if (item == TypeCategory::INT64) 211 args.push_back(type);
183 { 212 }
184 signature[pos++] = 'i'; 213
185 signature[pos++] = 'i'; 214 get_function_name(function, signature.c_str());
186 }
187 else if (item == TypeCategory::FLOAT)
188 signature[pos++] = 'd';
189 else
190 throw std::runtime_error("Unexpected function argument type");
191 args.push_back(item);
192 }
193
194 signature[pos] = 0;
195
196 get_function_name(function, signature);
197 } 215 }
198 216
199 template<typename ReturnType, typename... Args> 217 template<typename ReturnType, typename... Args>
200 FunctionInfo(ReturnType (*function)(Args...)) 218 FunctionInfo(ReturnType (*function)(Args...))
201 : FunctionInfo(TypeInfo<ReturnType>(), 219 : FunctionInfo(TypeInfo<ReturnType>(),
202 TypeInfo<ReturnType>().pointer_type(), { TypeInfo<Args>()... }, false, 220 TypeInfo<ReturnType>().pointer_type(), { TypeInfo<Args>()... }, false,
203 &function) 221 &function)
204 { 222 {
205 } 223 }
206 224
(...skipping 11 matching lines...) Expand all
218 TypeInfo<ReturnType>().pointer_type(), { TypeInfo<Args>()... }, true, 236 TypeInfo<ReturnType>().pointer_type(), { TypeInfo<Args>()... }, true,
219 &function) 237 &function)
220 { 238 {
221 } 239 }
222 240
223 bool empty() const 241 bool empty() const
224 { 242 {
225 return name[0] == '\0'; 243 return name[0] == '\0';
226 } 244 }
227 245
228 void get_function_name(void* ptr, char* signature) 246 void get_function_name(void* ptr, const char* signature)
229 { 247 {
230 // This is a hack, C++ won't let us get the mangled function name. 248 // This is a hack, C++ won't let us get the mangled function name.
231 // JavaScript is more dynamic so we pass the pointer to our function 249 // JavaScript is more dynamic so we pass the pointer to our function
232 // there. With that and the function signature we can call the function - 250 // there. With that and the function signature we can call the function -
233 // with a full stack so that we will cause it to abort. Sometimes the 251 // with a full stack so that we will cause it to abort. Sometimes the
234 // function we are calling will also be missing from the build. The result 252 // function we are calling will also be missing from the build. The result
235 // is the same: abort() is called which in turn calls stackTrace(). By 253 // is the same: abort() is called which in turn calls stackTrace(). By
236 // replacing stackTrace() we get access to the call stack and search it 254 // replacing stackTrace() we get access to the call stack and search it
237 // for the name of our function. 255 // for the name of our function.
238 256
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after
409 std::string call_str(call.name); 427 std::string call_str(call.name);
410 call_str += "("; 428 call_str += "(";
411 for (int i = 0; i < params.size(); i++) 429 for (int i = 0; i < params.size(); i++)
412 { 430 {
413 if (i > 0) 431 if (i > 0)
414 call_str += ", "; 432 call_str += ", ";
415 call_str += params[i]; 433 call_str += params[i];
416 } 434 }
417 call_str += ")"; 435 call_str += ")";
418 436
419 if (call.returnType == TypeCategory::VOID) 437 switch (call.returnType)
420 return " " + call_str + ";\n"; 438 {
421 else if (call.returnType == TypeCategory::INT || 439 case TypeCategory::VOID:
422 call.returnType == TypeCategory::FLOAT) 440 return " " + call_str + ";\n";
423 { 441 case TypeCategory::INT:
424 return " var result = " + call_str + ";\n"; 442 case TypeCategory::FLOAT:
425 } 443 case TypeCategory::DOUBLE:
426 else if (call.returnType == TypeCategory::INT64) 444 return " var result = " + call_str + ";\n";
427 { 445 case TypeCategory::INT64:
428 // Emscripten saves the high bits of a 64-bit return value in a special 446 return " var result = Runtime.makeBigInt(" + call_str + ", " +
429 // variable called tempRet0. We cannot use bit operators to combine the 447 "Runtime.getTempRet0(), " +
430 // values because JavaScript operates on 32-bit integers. 448 "true);\n";
431 return " var result = (" + call_str + " >>> 0)" + 449 case TypeCategory::DEPENDENT_STRING:
432 " + (Runtime.getTempRet0() >>> 0) * 0x100000000;\n"; 450 case TypeCategory::OWNED_STRING:
433 } 451 {
434 else if (call.returnType == TypeCategory::DEPENDENT_STRING || 452 std::string result;
435 call.returnType == TypeCategory::OWNED_STRING) 453 result += " var string = createString();\n";
436 { 454 result += " " + call_str + ";\n";
437 std::string result; 455 result += " var result = readString(string);\n";
438 result += " var string = createString();\n"; 456 if (call.returnType == TypeCategory::OWNED_STRING)
439 result += " " + call_str + ";\n"; 457 result += " Module._DestroyString(string);\n";
440 result += " var result = readString(string);\n"; 458 return result;
441 if (call.returnType == TypeCategory::OWNED_STRING) 459 }
442 result += " Module._DestroyString(string);\n"; 460 case TypeCategory::STRING_REF:
443 return result; 461 return " var result = readString(" + call_str + ");\n";
444 } 462 case TypeCategory::CLASS_PTR:
445 else if (call.returnType == TypeCategory::STRING_REF) 463 {
446 { 464 std::string result;
447 return " var result = readString(" + call_str + ");\n"; 465 result += " var result = " + call_str + ";\n";
448 } 466 result += " if (result)\n";
449 else if (call.returnType == TypeCategory::CLASS_PTR) 467 result += " {\n";
450 { 468
451 std::string result; 469 auto it = classes.find(call.pointerType);
452 result += " var result = " + call_str + ";\n"; 470 if (it == classes.end())
453 result += " if (result)\n"; 471 throw std::runtime_error("Function " + std::string(call.name) + " retu rns pointer to unknown class");
454 result += " {\n"; 472
455 473 const ClassInfo& cls = it->second;
456 auto it = classes.find(call.pointerType); 474 auto offset = cls.subclass_differentiator.offset;
457 if (it == classes.end()) 475 if (offset == SIZE_MAX)
458 throw std::runtime_error("Function " + std::string(call.name) + " return s pointer to unknown class"); 476 result += " result = exports." + cls.name + "(result);\n";
459 477 else
460 const ClassInfo& cls = it->second; 478 result += " result = exports." + cls.name + ".fromPointer(result);\ n";
461 auto offset = cls.subclass_differentiator.offset; 479
462 if (offset == SIZE_MAX) 480 result += " }\n";
463 result += " result = exports." + cls.name + "(result);\n"; 481 result += " else\n";
464 else 482 result += " result = null;\n";
465 result += " result = exports." + cls.name + ".fromPointer(result);\n" ; 483 return result;
466 484 }
467 result += " }\n"; 485 default:
468 result += " else\n"; 486 throw std::runtime_error("Unexpected return type for " + std::string(cal l.name));
469 result += " result = null;\n"; 487 }
470 return result;
471 }
472 else
473 throw std::runtime_error("Unexpected return type for " + std::string(call. name));
474 } 488 }
475 489
476 const std::string wrapCall(const FunctionInfo& call) 490 const std::string wrapCall(const FunctionInfo& call)
477 { 491 {
478 bool hasStringArgs = false; 492 bool hasStringArgs = false;
479 std::vector<std::string> params; 493 std::vector<std::string> params;
480 std::string prefix = "function("; 494 std::string prefix = "function(";
481 for (int i = 0; i < call.args.size(); i++) 495 for (int i = 0; i < call.args.size(); i++)
482 { 496 {
483 std::string argName("arg" + std::to_string(i)); 497 std::string argName("arg" + std::to_string(i));
(...skipping 297 matching lines...) Expand 10 before | Expand all | Expand 10 after
781 bindings_internal::register_differentiator( 795 bindings_internal::register_differentiator(
782 bindings_internal::TypeInfo<ClassType>(), offset, mapping); 796 bindings_internal::TypeInfo<ClassType>(), offset, mapping);
783 return *this; 797 return *this;
784 } 798 }
785 }; 799 };
786 800
787 void custom_generator(bindings_internal::CustomGenerator generator) 801 void custom_generator(bindings_internal::CustomGenerator generator)
788 { 802 {
789 bindings_internal::customGenerators.push_back(generator); 803 bindings_internal::customGenerators.push_back(generator);
790 } 804 }
LEFTRIGHT

Powered by Google App Engine
This is Rietveld