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 for (size_t i = 0; i < 8; ++i) { |
| 16 UChar c = hexDigits[number & 0xF]; |
| 17 destination->append(c); |
| 18 number >>= 4; |
| 19 } |
| 20 } |
| 21 |
| 22 // Hash algorithm for substrings is described in "Über die Komplexität der |
| 23 // Multiplikation 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 static uint64_t prime[] = {0x3FB75161, 0xAB1F4E4F, 0x82675BC5, 0xCD924D35, |
| 28 0x81ABE279}; |
| 29 static uint64_t random[] = {0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, |
| 30 0xC3D2E1F0}; |
| 31 static uint32_t randomOdd[] = {0xB4663807, 0xCC322BF5, 0xD4F91BBD, 0xA7BEA11D, |
| 32 0x8F462907}; |
| 33 |
| 34 uint64_t hashes[] = {0, 0, 0, 0, 0}; |
| 35 uint64_t zi[] = {1, 1, 1, 1, 1}; |
| 36 |
| 37 const size_t hashesSize = V8_INSPECTOR_ARRAY_LENGTH(hashes); |
| 38 |
| 39 size_t current = 0; |
| 40 const uint32_t* data = nullptr; |
| 41 size_t sizeInBytes = sizeof(UChar) * str.length(); |
| 42 data = reinterpret_cast<const uint32_t*>(str.characters16()); |
| 43 for (size_t i = 0; i < sizeInBytes / 4; i += 4) { |
| 44 uint32_t v = data[i]; |
| 45 uint64_t xi = v * randomOdd[current] & 0x7FFFFFFF; |
| 46 hashes[current] = (hashes[current] + zi[current] * xi) % prime[current]; |
| 47 zi[current] = (zi[current] * random[current]) % prime[current]; |
| 48 current = current == hashesSize - 1 ? 0 : current + 1; |
| 49 } |
| 50 if (sizeInBytes % 4) { |
| 51 uint32_t v = 0; |
| 52 for (size_t i = sizeInBytes - sizeInBytes % 4; i < sizeInBytes; ++i) { |
| 53 v <<= 8; |
| 54 v |= reinterpret_cast<const uint8_t*>(data)[i]; |
| 55 } |
| 56 uint64_t xi = v * randomOdd[current] & 0x7FFFFFFF; |
| 57 hashes[current] = (hashes[current] + zi[current] * xi) % prime[current]; |
| 58 zi[current] = (zi[current] * random[current]) % prime[current]; |
| 59 current = current == hashesSize - 1 ? 0 : current + 1; |
| 60 } |
| 61 |
| 62 for (size_t i = 0; i < hashesSize; ++i) |
| 63 hashes[i] = (hashes[i] + zi[i] * (prime[i] - 1)) % prime[i]; |
| 64 |
| 65 String16Builder hash; |
| 66 for (size_t i = 0; i < hashesSize; ++i) appendUnsignedAsHex(hashes[i], &hash); |
| 67 return hash.toString(); |
| 68 } |
| 69 |
| 70 V8DebuggerScript::V8DebuggerScript(v8::Isolate* isolate, |
| 71 v8::Local<v8::Object> object, |
| 72 bool isLiveEdit) { |
| 73 v8::Local<v8::Value> idValue = |
| 74 object->Get(toV8StringInternalized(isolate, "id")); |
| 75 DCHECK(!idValue.IsEmpty() && idValue->IsInt32()); |
| 76 m_id = String16::fromInteger(idValue->Int32Value()); |
| 77 |
| 78 m_url = toProtocolStringWithTypeCheck( |
| 79 object->Get(toV8StringInternalized(isolate, "name"))); |
| 80 m_sourceURL = toProtocolStringWithTypeCheck( |
| 81 object->Get(toV8StringInternalized(isolate, "sourceURL"))); |
| 82 m_sourceMappingURL = toProtocolStringWithTypeCheck( |
| 83 object->Get(toV8StringInternalized(isolate, "sourceMappingURL"))); |
| 84 m_startLine = object->Get(toV8StringInternalized(isolate, "startLine")) |
| 85 ->ToInteger(isolate) |
| 86 ->Value(); |
| 87 m_startColumn = object->Get(toV8StringInternalized(isolate, "startColumn")) |
| 88 ->ToInteger(isolate) |
| 89 ->Value(); |
| 90 m_endLine = object->Get(toV8StringInternalized(isolate, "endLine")) |
| 91 ->ToInteger(isolate) |
| 92 ->Value(); |
| 93 m_endColumn = object->Get(toV8StringInternalized(isolate, "endColumn")) |
| 94 ->ToInteger(isolate) |
| 95 ->Value(); |
| 96 m_executionContextAuxData = toProtocolStringWithTypeCheck( |
| 97 object->Get(toV8StringInternalized(isolate, "executionContextAuxData"))); |
| 98 m_executionContextId = |
| 99 object->Get(toV8StringInternalized(isolate, "executionContextId")) |
| 100 ->ToInteger(isolate) |
| 101 ->Value(); |
| 102 m_isLiveEdit = isLiveEdit; |
| 103 |
| 104 v8::Local<v8::Value> sourceValue = |
| 105 object->Get(toV8StringInternalized(isolate, "source")); |
| 106 if (!sourceValue.IsEmpty() && sourceValue->IsString()) |
| 107 setSource(isolate, sourceValue.As<v8::String>()); |
| 108 } |
| 109 |
| 110 V8DebuggerScript::~V8DebuggerScript() {} |
| 111 |
| 112 const String16& V8DebuggerScript::sourceURL() const { |
| 113 return m_sourceURL.isEmpty() ? m_url : m_sourceURL; |
| 114 } |
| 115 |
| 116 v8::Local<v8::String> V8DebuggerScript::source(v8::Isolate* isolate) const { |
| 117 return m_source.Get(isolate); |
| 118 } |
| 119 |
| 120 void V8DebuggerScript::setSourceURL(const String16& sourceURL) { |
| 121 m_sourceURL = sourceURL; |
| 122 } |
| 123 |
| 124 void V8DebuggerScript::setSourceMappingURL(const String16& sourceMappingURL) { |
| 125 m_sourceMappingURL = sourceMappingURL; |
| 126 } |
| 127 |
| 128 void V8DebuggerScript::setSource(v8::Isolate* isolate, |
| 129 v8::Local<v8::String> source) { |
| 130 m_source.Reset(isolate, source); |
| 131 m_hash = calculateHash(toProtocolString(source)); |
| 132 } |
| 133 |
| 134 } // namespace v8_inspector |
OLD | NEW |