OLD | NEW |
---|---|
1 // Copyright 2013 the V8 project authors. All rights reserved. | 1 // Copyright 2013 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 15 matching lines...) Expand all Loading... | |
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | 27 |
28 #include "icu_util.h" | 28 #include "icu_util.h" |
29 | 29 |
30 #if defined(_WIN32) | 30 #if defined(_WIN32) |
31 #include <windows.h> | 31 #include <windows.h> |
32 #endif | 32 #endif |
33 | 33 |
34 #if defined(V8_I18N_SUPPORT) | 34 #if defined(V8_I18N_SUPPORT) |
35 #include <stdio.h> | 35 #include <stdio.h> |
36 #include <stdlib.h> | |
36 | 37 |
37 #include "unicode/putil.h" | 38 #include "unicode/putil.h" |
38 #include "unicode/udata.h" | 39 #include "unicode/udata.h" |
39 | 40 |
40 #define ICU_UTIL_DATA_FILE 0 | 41 #define ICU_UTIL_DATA_FILE 0 |
41 #define ICU_UTIL_DATA_SHARED 1 | 42 #define ICU_UTIL_DATA_SHARED 1 |
42 #define ICU_UTIL_DATA_STATIC 2 | 43 #define ICU_UTIL_DATA_STATIC 2 |
43 | 44 |
44 #define ICU_UTIL_DATA_SYMBOL "icudt" U_ICU_VERSION_SHORT "_dat" | 45 #define ICU_UTIL_DATA_SYMBOL "icudt" U_ICU_VERSION_SHORT "_dat" |
45 #define ICU_UTIL_DATA_SHARED_MODULE_NAME "icudt.dll" | 46 #define ICU_UTIL_DATA_SHARED_MODULE_NAME "icudt.dll" |
46 #endif | 47 #endif |
47 | 48 |
48 namespace v8 { | 49 namespace v8 { |
49 | 50 |
50 namespace internal { | 51 namespace internal { |
51 | 52 |
53 #if ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_FILE | |
54 namespace { | |
55 char* g_icu_data_ptr = NULL; | |
56 | |
57 void free_icu_data_ptr() { | |
58 delete[] g_icu_data_ptr; | |
59 } | |
60 | |
61 } // namespace | |
62 #endif | |
63 | |
52 bool InitializeICU(const char* icu_data_file) { | 64 bool InitializeICU(const char* icu_data_file) { |
53 #if !defined(V8_I18N_SUPPORT) | 65 #if !defined(V8_I18N_SUPPORT) |
54 return true; | 66 return true; |
55 #else | 67 #else |
56 #if ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_SHARED | 68 #if ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_SHARED |
57 // We expect to find the ICU data module alongside the current module. | 69 // We expect to find the ICU data module alongside the current module. |
58 HMODULE module = LoadLibraryA(ICU_UTIL_DATA_SHARED_MODULE_NAME); | 70 HMODULE module = LoadLibraryA(ICU_UTIL_DATA_SHARED_MODULE_NAME); |
59 if (!module) return false; | 71 if (!module) return false; |
60 | 72 |
61 FARPROC addr = GetProcAddress(module, ICU_UTIL_DATA_SYMBOL); | 73 FARPROC addr = GetProcAddress(module, ICU_UTIL_DATA_SYMBOL); |
62 if (!addr) return false; | 74 if (!addr) return false; |
63 | 75 |
64 UErrorCode err = U_ZERO_ERROR; | 76 UErrorCode err = U_ZERO_ERROR; |
65 udata_setCommonData(reinterpret_cast<void*>(addr), &err); | 77 udata_setCommonData(reinterpret_cast<void*>(addr), &err); |
66 return err == U_ZERO_ERROR; | 78 return err == U_ZERO_ERROR; |
67 #elif ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_STATIC | 79 #elif ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_STATIC |
68 // Mac/Linux bundle the ICU data in. | 80 // Mac/Linux bundle the ICU data in. |
69 return true; | 81 return true; |
70 #elif ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_FILE | 82 #elif ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_FILE |
71 if (!icu_data_file) return false; | 83 if (!icu_data_file) return false; |
72 | 84 |
85 if (g_icu_data_ptr) return true; | |
86 | |
73 FILE* inf = fopen(icu_data_file, "rb"); | 87 FILE* inf = fopen(icu_data_file, "rb"); |
74 if (!inf) return false; | 88 if (!inf) return false; |
75 | 89 |
76 fseek(inf, 0, SEEK_END); | 90 fseek(inf, 0, SEEK_END); |
77 size_t size = ftell(inf); | 91 size_t size = ftell(inf); |
78 rewind(inf); | 92 rewind(inf); |
79 | 93 |
80 char* addr = new char[size]; | 94 g_icu_data_ptr = new char[size]; |
81 if (fread(addr, 1, size, inf) != size) { | 95 if (fread(g_icu_data_ptr, 1, size, inf) != size) { |
82 delete[] addr; | 96 delete[] g_icu_data_ptr; |
97 g_icu_data_ptr = NULL; | |
83 fclose(inf); | 98 fclose(inf); |
84 return false; | 99 return false; |
85 } | 100 } |
86 fclose(inf); | 101 fclose(inf); |
102 | |
103 atexit(free_icu_data_ptr); | |
Sven Panne
2014/03/26 07:44:47
Hmmm, using atexit is always a kind of a hack, and
| |
104 | |
87 UErrorCode err = U_ZERO_ERROR; | 105 UErrorCode err = U_ZERO_ERROR; |
88 udata_setCommonData(reinterpret_cast<void*>(addr), &err); | 106 udata_setCommonData(reinterpret_cast<void*>(g_icu_data_ptr), &err); |
89 return err == U_ZERO_ERROR; | 107 return err == U_ZERO_ERROR; |
90 #endif | 108 #endif |
91 #endif | 109 #endif |
92 } | 110 } |
93 | 111 |
94 } } // namespace v8::internal | 112 } } // namespace v8::internal |
OLD | NEW |