| 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 = arraysize(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 static v8::Local<v8::Value> GetChecked(v8::Local<v8::Context> context, | |
| 71 v8::Local<v8::Object> object, | |
| 72 const char* name) { | |
| 73 return object | |
| 74 ->Get(context, toV8StringInternalized(context->GetIsolate(), name)) | |
| 75 .ToLocalChecked(); | |
| 76 } | |
| 77 | |
| 78 static int64_t GetCheckedInt(v8::Local<v8::Context> context, | |
| 79 v8::Local<v8::Object> object, const char* name) { | |
| 80 return GetChecked(context, object, name) | |
| 81 ->ToInteger(context) | |
| 82 .ToLocalChecked() | |
| 83 ->Value(); | |
| 84 } | |
| 85 | |
| 86 V8DebuggerScript::V8DebuggerScript(v8::Local<v8::Context> context, | |
| 87 v8::Local<v8::Object> object, | |
| 88 bool isLiveEdit) { | |
| 89 v8::Isolate* isolate = context->GetIsolate(); | |
| 90 v8::Local<v8::Value> idValue = GetChecked(context, object, "id"); | |
| 91 DCHECK(!idValue.IsEmpty() && idValue->IsInt32()); | |
| 92 m_id = String16::fromInteger(idValue->Int32Value(context).FromJust()); | |
| 93 | |
| 94 m_url = toProtocolStringWithTypeCheck(GetChecked(context, object, "name")); | |
| 95 m_sourceURL = | |
| 96 toProtocolStringWithTypeCheck(GetChecked(context, object, "sourceURL")); | |
| 97 m_sourceMappingURL = toProtocolStringWithTypeCheck( | |
| 98 GetChecked(context, object, "sourceMappingURL")); | |
| 99 m_startLine = GetCheckedInt(context, object, "startLine"); | |
| 100 m_startColumn = GetCheckedInt(context, object, "startColumn"); | |
| 101 m_endLine = GetCheckedInt(context, object, "endLine"); | |
| 102 m_endColumn = GetCheckedInt(context, object, "endColumn"); | |
| 103 m_executionContextAuxData = toProtocolStringWithTypeCheck( | |
| 104 GetChecked(context, object, "executionContextAuxData")); | |
| 105 m_executionContextId = GetCheckedInt(context, object, "executionContextId"); | |
| 106 m_isLiveEdit = isLiveEdit; | |
| 107 | |
| 108 v8::Local<v8::Value> sourceValue; | |
| 109 if (!object->Get(context, toV8StringInternalized(isolate, "source")) | |
| 110 .ToLocal(&sourceValue) || | |
| 111 !sourceValue->IsString()) | |
| 112 return; | |
| 113 setSource(isolate, sourceValue.As<v8::String>()); | |
| 114 } | |
| 115 | |
| 116 V8DebuggerScript::~V8DebuggerScript() {} | |
| 117 | |
| 118 const String16& V8DebuggerScript::sourceURL() const { | |
| 119 return m_sourceURL.isEmpty() ? m_url : m_sourceURL; | |
| 120 } | |
| 121 | |
| 122 v8::Local<v8::String> V8DebuggerScript::source(v8::Isolate* isolate) const { | |
| 123 return m_source.Get(isolate); | |
| 124 } | |
| 125 | |
| 126 void V8DebuggerScript::setSourceURL(const String16& sourceURL) { | |
| 127 m_sourceURL = sourceURL; | |
| 128 } | |
| 129 | |
| 130 void V8DebuggerScript::setSourceMappingURL(const String16& sourceMappingURL) { | |
| 131 m_sourceMappingURL = sourceMappingURL; | |
| 132 } | |
| 133 | |
| 134 void V8DebuggerScript::setSource(v8::Isolate* isolate, | |
| 135 v8::Local<v8::String> source) { | |
| 136 m_source.Reset(isolate, source); | |
| 137 m_hash = calculateHash(toProtocolString(source)); | |
| 138 } | |
| 139 | |
| 140 } // namespace v8_inspector | |
| OLD | NEW |