|
| 1 | +import re |
| 2 | + |
| 3 | +def mock(input_file): |
| 4 | + with open(input_file, 'r', encoding='utf-8') as file: |
| 5 | + lines = file.readlines() |
| 6 | + |
| 7 | + inside_extern_c = False |
| 8 | + add_mock_impl = False |
| 9 | + is_void = False |
| 10 | + should_delete = False |
| 11 | + string_cache_for_bool = '' |
| 12 | + string_cache = '' |
| 13 | + brace_count = 0 |
| 14 | + output = [] |
| 15 | + fn_name = '' |
| 16 | + |
| 17 | + for line in lines: |
| 18 | + if line.endswith('extern "C" {\n'): |
| 19 | + inside_extern_c = True |
| 20 | + brace_count += 1 |
| 21 | + string_cache += line |
| 22 | + |
| 23 | + elif inside_extern_c and 'static' in line and (line.endswith(': usize;\n') |
| 24 | + or line.endswith(': u64;\n') or line.endswith('::std::os::raw::c_char;\n')): |
| 25 | + add_mock_impl = True |
| 26 | + string_cache += line |
| 27 | + string_cache_for_bool += line[0:-2] |
| 28 | + string_cache_for_bool += ' = 0;\n' |
| 29 | + |
| 30 | + elif inside_extern_c and 'static' in line and ('ProxyClass:' in line |
| 31 | + or 'Wrapper_singleton:' in line or 'Wrapper_singletonWithPrototype:' in line |
| 32 | + or 'Wrapper_defaultProto:' in line |
| 33 | + or 'CrossCompartmentWrapper_singleton:' in line |
| 34 | + or 'CrossCompartmentWrapper_singletonWithPrototype:' in line |
| 35 | + or 'OpaqueCrossCompartmentWrapper_singleton:' in line |
| 36 | + or 'FalseHandleValue:' in line |
| 37 | + or 'ArrayBuffer_FixedLengthUnsharedClass:' in line |
| 38 | + or 'ArrayBuffer_ResizableUnsharedClass:' in line |
| 39 | + or 'ArrayBuffer_FixedLengthSharedClass:' in line |
| 40 | + or 'ArrayBuffer_GrowableSharedClass:' in line |
| 41 | + or 'DataView_FixedLengthClassPtr:' in line |
| 42 | + or 'DataView_ResizableClassPtr:' in line |
| 43 | + or 'TypedArray_base_fixedLengthClasses:' in line |
| 44 | + or 'TypedArray_base_resizableClasses:' in line |
| 45 | + or 'TypedArray_Scalar:' in line |
| 46 | + or 'JS_NULL_CLASS_SPEC:' in line or 'ProxyClassOps:' in line |
| 47 | + or 'JS_NULL_CLASS_EXT:' in line or 'ProxyClassExtension:' in line |
| 48 | + or 'JS_NULL_OBJECT_OPS:' in line or 'ProxyObjectOps:' in line |
| 49 | + or 'JS_NULL_CLASS_OPS:' in line): |
| 50 | + should_delete = True |
| 51 | + |
| 52 | + elif inside_extern_c and ('JS_CallArgsFromVp(' in line |
| 53 | + or 'LossyUTF8CharsToNewTwoByteCharsZ(' in line |
| 54 | + or 'SetGCSliceCallback(' in line |
| 55 | + or 'JS_GetStructuredCloneScope(' in line or 'LossyUTF8CharsToNewTwoByteCharsZ1(' in line |
| 56 | + or 'CompileGlobalScriptToStencil(' in line or 'LossyUTF8CharsToNewLatin1CharsZ(' in line |
| 57 | + or 'CompileGlobalScriptToStencil1(' in line or 'UTF8CharsToNewLatin1CharsZ(' in line |
| 58 | + or 'CompileModuleScriptToStencil(' in line or 'LossyTwoByteCharsToNewLatin1CharsZ(' in line |
| 59 | + or 'CompileModuleScriptToStencil1(' in line or 'UTF8CharsToNewTwoByteCharsZ(' in line |
| 60 | + or 'GetWarningReporter(' in line or 'UTF8CharsToNewTwoByteCharsZ1(' in line |
| 61 | + or 'GetModuleMetadataHook(' in line or 'SetWarningReporter(' in line |
| 62 | + or 'GetModuleDynamicImportHook(' in line or 'GetRealmObjectPrototypeHandle(' in line |
| 63 | + or 'GetReduceMicrosecondTimePrecisionCallback(' in line |
| 64 | + or 'GetJSTimers(' in line or 'GetWasmModule(' in line |
| 65 | + or 'SetDoCycleCollectionCallback(' in line or 'ReportBadValueTypeAndCrash(' in line |
| 66 | + or 'GetDebuggerMallocSizeOf(' in line or 'MaybeGetScriptPrivate(' in line |
| 67 | + or 'GetWellKnownSymbolKey(' in line): |
| 68 | + should_delete = True |
| 69 | + |
| 70 | + elif inside_extern_c and line.endswith('-> bool;\n'): |
| 71 | + if 'pub fn ' in line: |
| 72 | + match = re.search(r'.*?pub fn (\w+)', line) |
| 73 | + fn_name = match.group(1) |
| 74 | + add_mock_impl = True |
| 75 | + string_cache += line |
| 76 | + string_cache_for_bool += line[0:-2] |
| 77 | + string_cache_for_bool += ' { println!("' |
| 78 | + string_cache_for_bool += fn_name |
| 79 | + string_cache_for_bool += ' called"); true }\n' |
| 80 | + |
| 81 | + elif inside_extern_c and line.endswith('-> root::JS::SymbolCode;\n'): |
| 82 | + if 'pub fn ' in line: |
| 83 | + match = re.search(r'.*?pub fn (\w+)', line) |
| 84 | + fn_name = match.group(1) |
| 85 | + add_mock_impl = True |
| 86 | + string_cache += line |
| 87 | + string_cache_for_bool += line[0:-2] |
| 88 | + string_cache_for_bool += ' { println!("' |
| 89 | + string_cache_for_bool += fn_name |
| 90 | + string_cache_for_bool += ' called"); root::JS::SymbolCode::iterator }\n' |
| 91 | + |
| 92 | + elif inside_extern_c and line.endswith('-> root::JS::ModuleResolveHook;\n'): |
| 93 | + if 'pub fn ' in line: |
| 94 | + match = re.search(r'.*?pub fn (\w+)', line) |
| 95 | + fn_name = match.group(1) |
| 96 | + add_mock_impl = True |
| 97 | + string_cache += line |
| 98 | + string_cache_for_bool += line[0:-2] |
| 99 | + string_cache_for_bool += ' { println!("' |
| 100 | + string_cache_for_bool += fn_name |
| 101 | + string_cache_for_bool += ' called"); None }\n' |
| 102 | + |
| 103 | + elif inside_extern_c and line.endswith('-> root::JS::GCSliceCallback;\n'): |
| 104 | + if 'pub fn ' in line: |
| 105 | + match = re.search(r'.*?pub fn (\w+)', line) |
| 106 | + fn_name = match.group(1) |
| 107 | + add_mock_impl = True |
| 108 | + string_cache += line |
| 109 | + string_cache_for_bool += line[0:-2] |
| 110 | + string_cache_for_bool += ' { println!("' |
| 111 | + string_cache_for_bool += fn_name |
| 112 | + string_cache_for_bool += ' called"); None }\n' |
| 113 | + |
| 114 | + elif inside_extern_c and line.endswith('-> root::js::StackFormat;\n'): |
| 115 | + if 'pub fn ' in line: |
| 116 | + match = re.search(r'.*?pub fn (\w+)', line) |
| 117 | + fn_name = match.group(1) |
| 118 | + add_mock_impl = True |
| 119 | + string_cache += line |
| 120 | + string_cache_for_bool += line[0:-2] |
| 121 | + string_cache_for_bool += ' { println!("' |
| 122 | + string_cache_for_bool += fn_name |
| 123 | + string_cache_for_bool += ' called"); root::js::StackFormat::Default }\n' |
| 124 | + |
| 125 | + elif inside_extern_c and line.endswith('-> root::JS::Scalar::Type;\n'): |
| 126 | + if 'pub fn ' in line: |
| 127 | + match = re.search(r'.*?pub fn (\w+)', line) |
| 128 | + fn_name = match.group(1) |
| 129 | + add_mock_impl = True |
| 130 | + string_cache += line |
| 131 | + string_cache_for_bool += line[0:-2] |
| 132 | + string_cache_for_bool += ' { println!("' |
| 133 | + string_cache_for_bool += fn_name |
| 134 | + string_cache_for_bool += ' called"); root::JS::Scalar::Type::Int8 }\n' |
| 135 | + |
| 136 | + elif inside_extern_c and line.endswith('-> root::JSType;\n'): |
| 137 | + if 'pub fn ' in line: |
| 138 | + match = re.search(r'.*?pub fn (\w+)', line) |
| 139 | + fn_name = match.group(1) |
| 140 | + add_mock_impl = True |
| 141 | + string_cache += line |
| 142 | + string_cache_for_bool += line[0:-2] |
| 143 | + string_cache_for_bool += ' { println!("' |
| 144 | + string_cache_for_bool += fn_name |
| 145 | + string_cache_for_bool += ' called"); root::JSType::JSTYPE_UNDEFINED }\n' |
| 146 | + |
| 147 | + elif inside_extern_c and line.endswith('-> root::JS::TraceKind;\n'): |
| 148 | + if 'pub fn ' in line: |
| 149 | + match = re.search(r'.*?pub fn (\w+)', line) |
| 150 | + fn_name = match.group(1) |
| 151 | + add_mock_impl = True |
| 152 | + string_cache += line |
| 153 | + string_cache_for_bool += line[0:-2] |
| 154 | + string_cache_for_bool += ' { println!("' |
| 155 | + string_cache_for_bool += fn_name |
| 156 | + string_cache_for_bool += ' called"); root::JS::TraceKind::Object }\n' |
| 157 | + |
| 158 | + elif inside_extern_c and line.endswith('-> root::JS::GCReason;\n'): |
| 159 | + if 'pub fn ' in line: |
| 160 | + match = re.search(r'.*?pub fn (\w+)', line) |
| 161 | + fn_name = match.group(1) |
| 162 | + add_mock_impl = True |
| 163 | + string_cache += line |
| 164 | + string_cache_for_bool += line[0:-2] |
| 165 | + string_cache_for_bool += ' { println!("' |
| 166 | + string_cache_for_bool += fn_name |
| 167 | + string_cache_for_bool += ' called"); root::JS::GCReason::API }\n' |
| 168 | + |
| 169 | + elif inside_extern_c and line.endswith('-> root::JS::PromiseState;\n'): |
| 170 | + if 'pub fn ' in line: |
| 171 | + match = re.search(r'.*?pub fn (\w+)', line) |
| 172 | + fn_name = match.group(1) |
| 173 | + add_mock_impl = True |
| 174 | + string_cache += line |
| 175 | + string_cache_for_bool += line[0:-2] |
| 176 | + string_cache_for_bool += ' { println!("' |
| 177 | + string_cache_for_bool += fn_name |
| 178 | + string_cache_for_bool += ' called"); root::JS::PromiseState::Fulfilled }\n' |
| 179 | + |
| 180 | + elif inside_extern_c and line.endswith('-> root::JS::SavedFrameResult;\n'): |
| 181 | + if 'pub fn ' in line: |
| 182 | + match = re.search(r'.*?pub fn (\w+)', line) |
| 183 | + fn_name = match.group(1) |
| 184 | + add_mock_impl = True |
| 185 | + string_cache += line |
| 186 | + string_cache_for_bool += line[0:-2] |
| 187 | + string_cache_for_bool += ' { println!("' |
| 188 | + string_cache_for_bool += fn_name |
| 189 | + string_cache_for_bool += ' called"); root::JS::SavedFrameResult::Ok }\n' |
| 190 | + |
| 191 | + elif inside_extern_c and line.endswith('-> root::JS::TranscodeResult;\n'): |
| 192 | + if 'pub fn ' in line: |
| 193 | + match = re.search(r'.*?pub fn (\w+)', line) |
| 194 | + fn_name = match.group(1) |
| 195 | + add_mock_impl = True |
| 196 | + string_cache += line |
| 197 | + string_cache_for_bool += line[0:-2] |
| 198 | + string_cache_for_bool += ' { println!("' |
| 199 | + string_cache_for_bool += fn_name |
| 200 | + string_cache_for_bool += ' called"); root::JS::TranscodeResult::Ok }\n' |
| 201 | + |
| 202 | + elif inside_extern_c and line.endswith('-> root::JS::PromiseUserInputEventHandlingState;\n'): |
| 203 | + if 'pub fn ' in line: |
| 204 | + match = re.search(r'.*?pub fn (\w+)', line) |
| 205 | + fn_name = match.group(1) |
| 206 | + add_mock_impl = True |
| 207 | + string_cache += line |
| 208 | + string_cache_for_bool += line[0:-2] |
| 209 | + string_cache_for_bool += ' { println!("' |
| 210 | + string_cache_for_bool += fn_name |
| 211 | + string_cache_for_bool += ' called"); root::JS::PromiseUserInputEventHandlingState::DontCare }\n' |
| 212 | + |
| 213 | + elif inside_extern_c and line.endswith('-> root::JSProtoKey;\n'): |
| 214 | + if 'pub fn ' in line: |
| 215 | + match = re.search(r'.*?pub fn (\w+)', line) |
| 216 | + fn_name = match.group(1) |
| 217 | + add_mock_impl = True |
| 218 | + string_cache += line |
| 219 | + string_cache_for_bool += line[0:-2] |
| 220 | + string_cache_for_bool += ' { println!("' |
| 221 | + string_cache_for_bool += fn_name |
| 222 | + string_cache_for_bool += ' called"); root::JSProtoKey::JSProto_Null }\n' |
| 223 | + |
| 224 | + elif inside_extern_c and line.endswith('-> root::JS::SmallestEncoding;\n'): |
| 225 | + if 'pub fn ' in line: |
| 226 | + match = re.search(r'.*?pub fn (\w+)', line) |
| 227 | + fn_name = match.group(1) |
| 228 | + add_mock_impl = True |
| 229 | + string_cache += line |
| 230 | + string_cache_for_bool += line[0:-2] |
| 231 | + string_cache_for_bool += ' { println!("' |
| 232 | + string_cache_for_bool += fn_name |
| 233 | + string_cache_for_bool += ' called"); root::JS::SmallestEncoding::ASCII }\n' |
| 234 | + |
| 235 | + elif inside_extern_c and line.endswith('-> root::JS::HeapState;\n'): |
| 236 | + if 'pub fn ' in line: |
| 237 | + match = re.search(r'.*?pub fn (\w+)', line) |
| 238 | + fn_name = match.group(1) |
| 239 | + add_mock_impl = True |
| 240 | + string_cache += line |
| 241 | + string_cache_for_bool += line[0:-2] |
| 242 | + string_cache_for_bool += ' { println!("' |
| 243 | + string_cache_for_bool += fn_name |
| 244 | + string_cache_for_bool += ' called"); root::JS::HeapState::Idle }\n' |
| 245 | + |
| 246 | + elif inside_extern_c and line.endswith('-> root::JSExnType;\n'): |
| 247 | + if 'pub fn ' in line: |
| 248 | + match = re.search(r'.*?pub fn (\w+)', line) |
| 249 | + fn_name = match.group(1) |
| 250 | + add_mock_impl = True |
| 251 | + string_cache += line |
| 252 | + string_cache_for_bool += line[0:-2] |
| 253 | + string_cache_for_bool += ' { println!("' |
| 254 | + string_cache_for_bool += fn_name |
| 255 | + string_cache_for_bool += ' called"); root::JSExnType::JSEXN_ERR }\n' |
| 256 | + |
| 257 | + elif inside_extern_c and (r' -> *mut' in line or r') -> *const' in line): |
| 258 | + if 'pub fn ' in line: |
| 259 | + match = re.search(r'.*?pub fn (\w+)', line) |
| 260 | + fn_name = match.group(1) |
| 261 | + add_mock_impl = True |
| 262 | + string_cache += line |
| 263 | + string_cache_for_bool += line[0:-2] |
| 264 | + string_cache_for_bool += ' { println!("' |
| 265 | + string_cache_for_bool += fn_name |
| 266 | + string_cache_for_bool += ' called"); ::std::ptr::null_mut() }\n' |
| 267 | + |
| 268 | + elif inside_extern_c and (line.endswith('-> i32;\n') |
| 269 | + or line.endswith('-> u16;\n') or line.endswith('-> usize;\n') |
| 270 | + or line.endswith('-> i64;\n') or line.endswith('-> u32;\n') |
| 271 | + or line.endswith('-> u64;\n') or line.endswith('-> root::JS::UniqueChars;\n') |
| 272 | + or line.endswith('-> root::JS::UniqueTwoByteChars;\n') |
| 273 | + or line.endswith('-> root::JS::UniqueLatin1Chars;\n') |
| 274 | + or line.endswith('-> root::JS::UniqueWideChars;\n')): |
| 275 | + if 'pub fn ' in line: |
| 276 | + match = re.search(r'.*?pub fn (\w+)', line) |
| 277 | + fn_name = match.group(1) |
| 278 | + add_mock_impl = True |
| 279 | + string_cache += line |
| 280 | + string_cache_for_bool += line[0:-2] |
| 281 | + string_cache_for_bool += ' { println!("' |
| 282 | + string_cache_for_bool += fn_name |
| 283 | + string_cache_for_bool += ' called"); 0 }\n' |
| 284 | + |
| 285 | + elif inside_extern_c and line.endswith('-> f64;\n'): |
| 286 | + if 'pub fn ' in line: |
| 287 | + match = re.search(r'.*?pub fn (\w+)', line) |
| 288 | + fn_name = match.group(1) |
| 289 | + add_mock_impl = True |
| 290 | + string_cache += line |
| 291 | + string_cache_for_bool += line[0:-2] |
| 292 | + string_cache_for_bool += ' { println!("' |
| 293 | + string_cache_for_bool += fn_name |
| 294 | + string_cache_for_bool += ' called"); 0.0 }\n' |
| 295 | + |
| 296 | + elif inside_extern_c and line.endswith('-> ::std::os::raw::c_uint;\n'): |
| 297 | + if 'pub fn ' in line: |
| 298 | + match = re.search(r'.*?pub fn (\w+)', line) |
| 299 | + fn_name = match.group(1) |
| 300 | + add_mock_impl = True |
| 301 | + string_cache += line |
| 302 | + string_cache_for_bool += line[0:-2] |
| 303 | + string_cache_for_bool += ' { println!("' |
| 304 | + string_cache_for_bool += fn_name |
| 305 | + string_cache_for_bool += ' called"); 0 }\n' |
| 306 | + |
| 307 | + elif inside_extern_c and line.endswith(');\n'): |
| 308 | + if 'pub fn ' in line: |
| 309 | + match = re.search(r'.*?pub fn (\w+)', line) |
| 310 | + fn_name = match.group(1) |
| 311 | + add_mock_impl = True |
| 312 | + string_cache += line |
| 313 | + string_cache_for_bool += line[0:-2] |
| 314 | + string_cache_for_bool += ' { println!("' |
| 315 | + string_cache_for_bool += fn_name |
| 316 | + string_cache_for_bool += ' called");}\n' |
| 317 | + |
| 318 | + elif r'#[link_name = ' in line: |
| 319 | + string_cache += line |
| 320 | + |
| 321 | + elif inside_extern_c and line.endswith('}\n'): |
| 322 | + brace_count -= 1 |
| 323 | + string_cache += line |
| 324 | + if brace_count == 0: |
| 325 | + if not should_delete: |
| 326 | + if not add_mock_impl: |
| 327 | + output.append(string_cache) |
| 328 | + else: |
| 329 | + output.append(string_cache_for_bool) |
| 330 | + inside_extern_c = False |
| 331 | + string_cache = '' |
| 332 | + string_cache_for_bool = '' |
| 333 | + add_mock_impl = False |
| 334 | + is_void = False |
| 335 | + should_delete = False |
| 336 | + fn_name = '' |
| 337 | + |
| 338 | + elif inside_extern_c and line.endswith('{\n'): |
| 339 | + string_cache_for_bool += line |
| 340 | + string_cache += line |
| 341 | + brace_count += 1 |
| 342 | + |
| 343 | + elif inside_extern_c and 'pub fn ' in line: |
| 344 | + match = re.search(r'.*?pub fn (\w+)', line) |
| 345 | + fn_name = match.group(1) |
| 346 | + string_cache += line |
| 347 | + string_cache_for_bool += line |
| 348 | + |
| 349 | + elif inside_extern_c and line.endswith('...\n'): |
| 350 | + should_delete = True |
| 351 | + |
| 352 | + elif inside_extern_c and line.endswith('-> !;\n'): |
| 353 | + should_delete = True |
| 354 | + |
| 355 | + elif inside_extern_c: |
| 356 | + string_cache += line |
| 357 | + string_cache_for_bool += line |
| 358 | + |
| 359 | + else: |
| 360 | + is_end_with_line = line.endswith('\n') |
| 361 | + stripped_line = line.strip() |
| 362 | + output.append(line) |
| 363 | + |
| 364 | + with open(input_file, 'w', encoding='utf-8') as file: |
| 365 | + file.writelines(output) |
| 366 | + |
| 367 | + |
| 368 | + |
| 369 | +# 调用函数并传入输入文件路径 |
| 370 | +input_file = r'servo2025/components/mozjs-sys/src/jsapi.rs' # 你的输入文件名 |
| 371 | +mock(input_file) |
0 commit comments