Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: src/FileSystemJsObject.cpp

Issue 29531696: Issue 5568 - Improve string handling by splitting read strings into only ASCII and non-ASCII strings (Closed) Base URL: https://github.com/adblockplus/libadblockplus.git
Patch Set: Created Aug. 30, 2017, 4:55 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>, 2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-present eyeo GmbH 3 * Copyright (C) 2006-present eyeo GmbH
4 * 4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify 5 * Adblock Plus is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as 6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
8 * 8 *
9 * Adblock Plus is distributed in the hope that it will be useful, 9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details. 12 * GNU General Public License for more details.
13 * 13 *
14 * You should have received a copy of the GNU General Public License 14 * You should have received a copy of the GNU General Public License
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */ 16 */
17 17
18 #include <AdblockPlus/IFileSystem.h> 18 #include <AdblockPlus/IFileSystem.h>
19 #include <stdexcept> 19 #include <stdexcept>
20 #include <sstream> 20 #include <sstream>
21 #include <vector> 21 #include <vector>
22 22
23 #include <AdblockPlus/JsValue.h> 23 #include <AdblockPlus/JsValue.h>
24 #include "FileSystemJsObject.h" 24 #include "FileSystemJsObject.h"
25 #include "JsContext.h" 25 #include "JsContext.h"
26 #include "Utils.h" 26 #include "Utils.h"
27 #include "JsError.h"
27 #include <AdblockPlus/Platform.h> 28 #include <AdblockPlus/Platform.h>
28 29
29 using namespace AdblockPlus; 30 using namespace AdblockPlus;
30 using AdblockPlus::Utils::ThrowExceptionInJS; 31 using AdblockPlus::Utils::ThrowExceptionInJS;
31 32
32 namespace 33 namespace
33 { 34 {
34 void ReadCallback(const v8::FunctionCallbackInfo<v8::Value>& arguments) 35 void ReadCallback(const v8::FunctionCallbackInfo<v8::Value>& arguments)
35 { 36 {
36 AdblockPlus::JsEnginePtr jsEngine = AdblockPlus::JsEngine::FromArguments(arg uments); 37 AdblockPlus::JsEnginePtr jsEngine = AdblockPlus::JsEngine::FromArguments(arg uments);
(...skipping 19 matching lines...) Expand all
56 57
57 const JsContext context(*jsEngine); 58 const JsContext context(*jsEngine);
58 auto result = jsEngine->NewObject(); 59 auto result = jsEngine->NewObject();
59 result.SetStringBufferProperty("content", std::move(content)); 60 result.SetStringBufferProperty("content", std::move(content));
60 if (!error.empty()) 61 if (!error.empty())
61 result.SetProperty("error", error); 62 result.SetProperty("error", error);
62 jsEngine->TakeJsValues(weakCallback)[0].Call(result); 63 jsEngine->TakeJsValues(weakCallback)[0].Call(result);
63 }); 64 });
64 } 65 }
65 66
67 inline bool CReturnOrLineFeed(char c)
hub 2017/08/30 21:13:31 nit': I would name this function "IsEndOfLine()"
sergei 2017/08/31 10:45:26 I am actually also not glad with the name of this
hub 2017/08/31 12:59:43 "End of line" or EOL is often used through the UNI
sergei 2017/08/31 13:21:44 agree.
68 {
69 return c == 10 || c == 13;
70 }
71
72 inline StringBuffer::const_iterator SkipCReturnAndLineFeed(StringBuffer::const _iterator ii, StringBuffer::const_iterator end)
hub 2017/08/30 21:13:30 and this one SkipEndOfLine()
73 {
74 while (ii != end && CReturnOrLineFeed(*ii))
75 ++ii;
76 return ii;
77 }
78
79 inline StringBuffer::const_iterator AdvanceUntilCReturnOrLineFeed(StringBuffer ::const_iterator ii, StringBuffer::const_iterator end)
hub 2017/08/30 21:13:31 And this one AdvanceToEndOfLine() But this is not
80 {
81 while (ii != end && !CReturnOrLineFeed(*ii))
82 ++ii;
83 return ii;
84 }
85
86 void ReadFromFileCallback(const v8::FunctionCallbackInfo<v8::Value>& arguments )
87 {
88 AdblockPlus::JsEnginePtr jsEngine = AdblockPlus::JsEngine::FromArguments(arg uments);
89 AdblockPlus::JsValueList converted = jsEngine->ConvertArguments(arguments);
90
91 v8::Isolate* isolate = arguments.GetIsolate();
92 if (converted.size() != 3)
93 return ThrowExceptionInJS(isolate, "_fileSystem.readFromFile requires 3 pa rameters");
94 if (!converted[1].IsFunction())
95 return ThrowExceptionInJS(isolate, "Second argument to _fileSystem.readFro mFile must be a function (listener callback)");
96 if (!converted[2].IsFunction())
97 return ThrowExceptionInJS(isolate, "Third argument to _fileSystem.readFrom File must be a function (done callback)");
98
99 JsValueList values;
100 values.push_back(converted[1]);
101 values.push_back(converted[2]);
102 auto weakCallback = jsEngine->StoreJsValues(values);
103 std::weak_ptr<JsEngine> weakJsEngine = jsEngine;
104 jsEngine->GetPlatform().GetFileSystem().Read(converted[0].AsString(),
105 [weakJsEngine, weakCallback]
106 (IFileSystem::IOBuffer&& content, const std::string& error)
107 {
108 auto jsEngine = weakJsEngine.lock();
109 if (!jsEngine)
110 return;
111
112 const JsContext context(*jsEngine);
113
114 auto jsValues = jsEngine->TakeJsValues(weakCallback);
115 if (!error.empty())
116 {
117 jsValues[1].Call(jsEngine->NewValue(error));
118 return;
119 }
120
121 auto processFunc = jsValues[0].UnwrapValue().As<v8::Function>();
122
123 auto globalContext = context.GetV8Context()->Global();
124 if (!globalContext->IsObject())
125 throw std::runtime_error("`this` pointer has to be an object");
126
127 const v8::TryCatch tryCatch;
128
129 const auto contentEnd = content.cend();
130 auto stringBegin = SkipCReturnAndLineFeed(content.begin(), contentEnd);
131 do
132 {
133 auto stringEnd = AdvanceUntilCReturnOrLineFeed(stringBegin, contentEnd);
134 auto jsLine = Utils::StringBufferToV8String(jsEngine->GetIsolate(), Stri ngBuffer(stringBegin, stringEnd)).As<v8::Value>();
135 processFunc->Call(globalContext, 1, &jsLine);
136 if (tryCatch.HasCaught())
137 {
138 jsValues[1].Call(jsEngine->NewValue(JsError::ExceptionToString(tryCatc h.Exception(), tryCatch.Message())));
139 return;
140 }
141 stringBegin = SkipCReturnAndLineFeed(stringEnd, contentEnd);
142 } while (stringBegin != contentEnd);
143 jsValues[1].Call();
144 });
145 }
146
66 void WriteCallback(const v8::FunctionCallbackInfo<v8::Value>& arguments) 147 void WriteCallback(const v8::FunctionCallbackInfo<v8::Value>& arguments)
67 { 148 {
68 AdblockPlus::JsEnginePtr jsEngine = AdblockPlus::JsEngine::FromArguments(arg uments); 149 AdblockPlus::JsEnginePtr jsEngine = AdblockPlus::JsEngine::FromArguments(arg uments);
69 AdblockPlus::JsValueList converted = jsEngine->ConvertArguments(arguments); 150 AdblockPlus::JsValueList converted = jsEngine->ConvertArguments(arguments);
70 151
71 v8::Isolate* isolate = arguments.GetIsolate(); 152 v8::Isolate* isolate = arguments.GetIsolate();
72 if (converted.size() != 3) 153 if (converted.size() != 3)
73 return ThrowExceptionInJS(isolate, "_fileSystem.write requires 3 parameter s"); 154 return ThrowExceptionInJS(isolate, "_fileSystem.write requires 3 parameter s");
74 if (!converted[2].IsFunction()) 155 if (!converted[2].IsFunction())
75 return ThrowExceptionInJS(isolate, "Third argument to _fileSystem.write mu st be a function"); 156 return ThrowExceptionInJS(isolate, "Third argument to _fileSystem.write mu st be a function");
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after
191 params.push_back(result); 272 params.push_back(result);
192 jsEngine->TakeJsValues(weakCallback)[0].Call(params); 273 jsEngine->TakeJsValues(weakCallback)[0].Call(params);
193 }); 274 });
194 } 275 }
195 } 276 }
196 277
197 278
198 JsValue& FileSystemJsObject::Setup(JsEngine& jsEngine, JsValue& obj) 279 JsValue& FileSystemJsObject::Setup(JsEngine& jsEngine, JsValue& obj)
199 { 280 {
200 obj.SetProperty("read", jsEngine.NewCallback(::ReadCallback)); 281 obj.SetProperty("read", jsEngine.NewCallback(::ReadCallback));
282 obj.SetProperty("readFromFile", jsEngine.NewCallback(::ReadFromFileCallback));
201 obj.SetProperty("write", jsEngine.NewCallback(::WriteCallback)); 283 obj.SetProperty("write", jsEngine.NewCallback(::WriteCallback));
202 obj.SetProperty("move", jsEngine.NewCallback(::MoveCallback)); 284 obj.SetProperty("move", jsEngine.NewCallback(::MoveCallback));
203 obj.SetProperty("remove", jsEngine.NewCallback(::RemoveCallback)); 285 obj.SetProperty("remove", jsEngine.NewCallback(::RemoveCallback));
204 obj.SetProperty("stat", jsEngine.NewCallback(::StatCallback)); 286 obj.SetProperty("stat", jsEngine.NewCallback(::StatCallback));
205 return obj; 287 return obj;
206 } 288 }
OLDNEW
« no previous file with comments | « lib/io.js ('k') | src/JsError.h » ('j') | test/FileSystemJsObject.cpp » ('J')

Powered by Google App Engine
This is Rietveld