OLD | NEW |
| (Empty) |
1 // Copyright 2014 the V8 project authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "src/inspector/V8DebuggerScript.h" | |
6 | |
7 #include "src/inspector/ProtocolPlatform.h" | |
8 #include "src/inspector/StringUtil.h" | |
9 | |
10 namespace v8_inspector { | |
11 | |
12 static const char hexDigits[17] = "0123456789ABCDEF"; | |
13 | |
14 static void appendUnsignedAsHex(unsigned number, String16Builder* destination) | |
15 { | |
16 for (size_t i = 0; i < 8; ++i) { | |
17 UChar c = hexDigits[number & 0xF]; | |
18 destination->append(c); | |
19 number >>= 4; | |
20 } | |
21 } | |
22 | |
23 // Hash algorithm for substrings is described in "Über die Komplexität der Multi
plikation in | |
24 // eingeschränkten Branchingprogrammmodellen" by Woelfe. | |
25 // http://opendatastructures.org/versions/edition-0.1d/ods-java/node33.html#SECT
ION00832000000000000000 | |
26 static String16 calculateHash(const String16& str) | |
27 { | |
28 static uint64_t prime[] = { 0x3FB75161, 0xAB1F4E4F, 0x82675BC5, 0xCD924D35,
0x81ABE279 }; | |
29 static uint64_t random[] = { 0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476,
0xC3D2E1F0 }; | |
30 static uint32_t randomOdd[] = { 0xB4663807, 0xCC322BF5, 0xD4F91BBD, 0xA7BEA1
1D, 0x8F462907 }; | |
31 | |
32 uint64_t hashes[] = { 0, 0, 0, 0, 0 }; | |
33 uint64_t zi[] = { 1, 1, 1, 1, 1 }; | |
34 | |
35 const size_t hashesSize = V8_INSPECTOR_ARRAY_LENGTH(hashes); | |
36 | |
37 size_t current = 0; | |
38 const uint32_t* data = nullptr; | |
39 size_t sizeInBytes = sizeof(UChar) * str.length(); | |
40 data = reinterpret_cast<const uint32_t*>(str.characters16()); | |
41 for (size_t i = 0; i < sizeInBytes / 4; i += 4) { | |
42 uint32_t v = data[i]; | |
43 uint64_t xi = v * randomOdd[current] & 0x7FFFFFFF; | |
44 hashes[current] = (hashes[current] + zi[current] * xi) % prime[current]; | |
45 zi[current] = (zi[current] * random[current]) % prime[current]; | |
46 current = current == hashesSize - 1 ? 0 : current + 1; | |
47 } | |
48 if (sizeInBytes % 4) { | |
49 uint32_t v = 0; | |
50 for (size_t i = sizeInBytes - sizeInBytes % 4; i < sizeInBytes; ++i) { | |
51 v <<= 8; | |
52 v |= reinterpret_cast<const uint8_t*>(data)[i]; | |
53 } | |
54 uint64_t xi = v * randomOdd[current] & 0x7FFFFFFF; | |
55 hashes[current] = (hashes[current] + zi[current] * xi) % prime[current]; | |
56 zi[current] = (zi[current] * random[current]) % prime[current]; | |
57 current = current == hashesSize - 1 ? 0 : current + 1; | |
58 } | |
59 | |
60 for (size_t i = 0; i < hashesSize; ++i) | |
61 hashes[i] = (hashes[i] + zi[i] * (prime[i] - 1)) % prime[i]; | |
62 | |
63 String16Builder hash; | |
64 for (size_t i = 0; i < hashesSize; ++i) | |
65 appendUnsignedAsHex(hashes[i], &hash); | |
66 return hash.toString(); | |
67 } | |
68 | |
69 V8DebuggerScript::V8DebuggerScript(v8::Isolate* isolate, v8::Local<v8::Object> o
bject, bool isLiveEdit) | |
70 { | |
71 v8::Local<v8::Value> idValue = object->Get(toV8StringInternalized(isolate, "
id")); | |
72 DCHECK(!idValue.IsEmpty() && idValue->IsInt32()); | |
73 m_id = String16::fromInteger(idValue->Int32Value()); | |
74 | |
75 m_url = toProtocolStringWithTypeCheck(object->Get(toV8StringInternalized(iso
late, "name"))); | |
76 m_sourceURL = toProtocolStringWithTypeCheck(object->Get(toV8StringInternaliz
ed(isolate, "sourceURL"))); | |
77 m_sourceMappingURL = toProtocolStringWithTypeCheck(object->Get(toV8StringInt
ernalized(isolate, "sourceMappingURL"))); | |
78 m_startLine = object->Get(toV8StringInternalized(isolate, "startLine"))->ToI
nteger(isolate)->Value(); | |
79 m_startColumn = object->Get(toV8StringInternalized(isolate, "startColumn"))-
>ToInteger(isolate)->Value(); | |
80 m_endLine = object->Get(toV8StringInternalized(isolate, "endLine"))->ToInteg
er(isolate)->Value(); | |
81 m_endColumn = object->Get(toV8StringInternalized(isolate, "endColumn"))->ToI
nteger(isolate)->Value(); | |
82 m_executionContextAuxData = toProtocolStringWithTypeCheck(object->Get(toV8St
ringInternalized(isolate, "executionContextAuxData"))); | |
83 m_executionContextId = object->Get(toV8StringInternalized(isolate, "executio
nContextId"))->ToInteger(isolate)->Value(); | |
84 m_isLiveEdit = isLiveEdit; | |
85 | |
86 v8::Local<v8::Value> sourceValue = object->Get(toV8StringInternalized(isolat
e, "source")); | |
87 if (!sourceValue.IsEmpty() && sourceValue->IsString()) | |
88 setSource(isolate, sourceValue.As<v8::String>()); | |
89 } | |
90 | |
91 V8DebuggerScript::~V8DebuggerScript() | |
92 { | |
93 } | |
94 | |
95 const String16& V8DebuggerScript::sourceURL() const | |
96 { | |
97 return m_sourceURL.isEmpty() ? m_url : m_sourceURL; | |
98 } | |
99 | |
100 v8::Local<v8::String> V8DebuggerScript::source(v8::Isolate* isolate) const | |
101 { | |
102 return m_source.Get(isolate); | |
103 } | |
104 | |
105 void V8DebuggerScript::setSourceURL(const String16& sourceURL) | |
106 { | |
107 m_sourceURL = sourceURL; | |
108 } | |
109 | |
110 void V8DebuggerScript::setSourceMappingURL(const String16& sourceMappingURL) | |
111 { | |
112 m_sourceMappingURL = sourceMappingURL; | |
113 } | |
114 | |
115 void V8DebuggerScript::setSource(v8::Isolate* isolate, v8::Local<v8::String> sou
rce) | |
116 { | |
117 m_source.Reset(isolate, source); | |
118 m_hash = calculateHash(toProtocolString(source)); | |
119 } | |
120 | |
121 } // namespace v8_inspector | |
OLD | NEW |