Skip to content

Commit 41e0fb3

Browse files
committed
updates
1 parent 9595b18 commit 41e0fb3

File tree

9 files changed

+130
-11
lines changed

9 files changed

+130
-11
lines changed

core/import.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ SOFTWARE.
3535
#include <cstring>
3636
#include <sstream>
3737
#include "utility.h"
38+
#include <utility>
3839

3940
std::string Import::ReadFile(const std::string path, const std::string position) {
4041
#ifndef __EMSCRIPTEN__
@@ -49,8 +50,8 @@ std::string Import::ReadFile(const std::string path, const std::string position)
4950
#endif
5051
}
5152

52-
ParseResult Import::JUSTC(const std::string path, const std::string position, const bool doExecute, const bool asynchronously) {
53+
std::pair<ParseResult, std::string> Import::JUSTC(const std::string path, const std::string position, const bool doExecute, const bool asynchronously) {
5354
std::string File = ReadFile(path, position);
5455
auto lexerResult = Lexer::parse(File);
55-
return Parser::parseTokens(lexerResult.second, doExecute, asynchronously, lexerResult.first);
56+
return {Parser::parseTokens(lexerResult.second, doExecute, asynchronously, lexerResult.first), File};
5657
}

core/import.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,13 @@ SOFTWARE.
3030
#include "parser.h"
3131
#include <string>
3232
#include <cstring>
33+
#include <utility>
3334

3435
class Import {
3536
private:
3637
static std::string ReadFile(const std::string path, const std::string position);
3738
public:
38-
static ParseResult JUSTC(const std::string path, const std::string position, const bool doExecute, const bool asynchronously);
39+
static std::pair<ParseResult, std::string> JUSTC(const std::string path, const std::string position, const bool doExecute, const bool asynchronously);
3940
};
4041

4142
#endif

core/parser.cpp

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -352,15 +352,20 @@ void Parser::addLog(const std::string& type, const std::string& message, size_t
352352
appendToLogFile("[" + time + "] " + message);
353353
}
354354
}
355-
356355
void Parser::setLogFile(const std::string& path) {
357356
logFilePath = path;
358357
hasLogFile = true;
359358
}
360-
361359
void Parser::appendToLogFile(const std::string& content) {
362360
logFileContent += content + "\n";
363361
}
362+
void Parser::addImportLog(const std::string& path, const std::string& script, const std::string& type) {
363+
std::vector<std::string> log;
364+
log.push_back(path);
365+
log.push_back(script);
366+
log.push_back(type);
367+
importLogs.push_back(log);
368+
}
364369

365370
ParserToken Parser::currentToken() const {
366371
if (position >= tokens.size()) {
@@ -637,12 +642,20 @@ ASTNode Parser::parseImportCommand() {
637642
else throw std::runtime_error("Expected \")\", got \"" + currentToken().value + "\" at " + Utility::position(position, input));
638643
// if (match("keyword", "REQUIRE") || match("keyword", "EXECUTE"));
639644

640-
ParseResult imports = Import::JUSTC(path, Utility::position(position, input), true, true);
641-
for (const auto& pair : imports.returnValues) {
645+
std::pair<ParseResult, std::string> imports = Import::JUSTC(path, Utility::position(position, input), true, true);
646+
addImportLog(path, imports.second, "JUSTC");
647+
for (const auto& pair : imports.first.returnValues) {
642648
ASTNode node = ASTNode("VARIABLE_DECLARATION", pair.first, position);
643649
node.value = pair.second;
644650
ast.push_back(node);
645651
}
652+
for (size_t i = 0; i < imports.first.importLogs.size(); i++) {
653+
std::vector<std::string> importLog = imports.first.importLogs[i];
654+
std::string _path = importLog[0];
655+
std::string _script = importLog[1];
656+
std::string _type = importLog[2];
657+
addImportLog(_path, _script, _type);
658+
}
646659

647660
} else {
648661
throw std::runtime_error("Expected \"(\", got \"" + currentToken().value + "\" at " + Utility::position(position, input));

core/parser.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ struct ParseResult {
130130
std::string logFilePath;
131131
std::string logFileContent;
132132
std::string error;
133+
std::vector<std::vector<std::string>> importLogs;
133134

134135
ParseResult() : logFilePath(""), logFileContent(""), error("") {}
135136
};
@@ -198,10 +199,13 @@ class Parser {
198199
return result;
199200
}
200201

202+
std::vector<std::vector<std::string>> importLogs;
203+
201204
// logs
202205
void addLog(const std::string& type, const std::string& message, size_t position = 0);
203206
void setLogFile(const std::string& path);
204207
void appendToLogFile(const std::string& content);
208+
void addImportLog(const std::string& path, const std::string& script, const std::string& type);
205209

206210
Value astNodeToValue(const ASTNode& node);
207211

core/to.json.cpp

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,11 @@ std::string JsonSerializer::serialize(const ParseResult& result) {
149149
json << "\"logfile\":{";
150150
json << "\"file\":\"" << escapeJsonString(result.logFilePath) << "\",";
151151
json << "\"logs\":\"" << escapeJsonString(result.logFileContent) << "\"";
152-
json << "}";
152+
json << "},";
153+
154+
// import logs array
155+
json << "\"imported\":";
156+
json << serialize(result.importLogs);
153157
}
154158

155159
#else
@@ -198,3 +202,29 @@ std::string JsonSerializer::serialize(const std::vector<LogEntry>& logs) {
198202
json << "]";
199203
return json.str();
200204
}
205+
206+
std::string JsonSerializer::serialize(const std::vector<std::vector<std::string>>& importLogs) {
207+
std::stringstream json;
208+
json << "[";
209+
210+
for (size_t i = 0; i < importLogs.size(); i++) {
211+
const auto& log = importLogs[i];
212+
213+
json << "[";
214+
for (size_t j = 0; j < log.size(); j++) {
215+
const std::string importLog = log[j];
216+
json << "\"" + escapeJsonString(importLog) + "\"";
217+
if (j < log.size() - 1) {
218+
json << ",";
219+
}
220+
}
221+
json << "]";
222+
223+
if (i < importLogs.size() - 1) {
224+
json << ",";
225+
}
226+
}
227+
228+
json << "]";
229+
return json.str();
230+
}

core/to.json.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ class JsonSerializer {
3535
static std::string serialize(const ParseResult& result);
3636
static std::string serialize(const std::vector<ParserToken>& tokens, const std::string& input);
3737
static std::string serialize(const std::vector<LogEntry>& logs);
38+
static std::string serialize(const std::vector<std::vector<std::string>>& importLogs);
3839
static std::string escapeJsonString(const std::string& str);
3940

4041
private:

core/to.xml.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,11 @@ std::string XmlSerializer::serialize(const ParseResult& result) {
138138
json << "\"logfile\":{";
139139
json << "\"file\":\"" << JsonSerializer::escapeJsonString(result.logFilePath) << "\",";
140140
json << "\"logs\":\"" << JsonSerializer::escapeJsonString(result.logFileContent) << "\"";
141-
json << "}";
141+
json << "},";
142+
143+
// import logs array
144+
json << "\"imported\":";
145+
json << JsonSerializer::serialize(result.importLogs);
142146

143147
json << "}";
144148
return json.str();

core/to.yaml.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,11 @@ std::string YamlSerializer::serialize(const ParseResult& result) {
156156
json << "\"logfile\":{";
157157
json << "\"file\":\"" << JsonSerializer::escapeJsonString(result.logFilePath) << "\",";
158158
json << "\"logs\":\"" << JsonSerializer::escapeJsonString(result.logFileContent) << "\"";
159-
json << "}";
159+
json << "},";
160+
161+
// import logs array
162+
json << "\"imported\":";
163+
json << JsonSerializer::serialize(result.importLogs);
160164

161165
json << "}";
162166
return json.str();

javascript/core.js

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ SOFTWARE.
7676
const ERR = Error;
7777
const CONSOLE = console;
7878
const MAP = Map;
79+
const BLOB = isBrowser ? Blob : null;
7980

8081
JUSTC.VERSION = null;
8182
JUSTC.GetVersion = function() {
@@ -105,7 +106,7 @@ SOFTWARE.
105106
};
106107

107108
if (isBrowser) {
108-
JUSTC.Checks.sysFunc(OBJECT, ARRAY, __URL__, STRING, ERR, MAP);
109+
JUSTC.Checks.sysFunc(OBJECT, ARRAY, __URL__, STRING, ERR, MAP, BLOB);
109110
JUSTC.Checks.sysObj(json_, CONSOLE);
110111
JUSTC.Checks.sysObj(globalThis_, DOCUMENT);
111112
JUSTC.Checks.sysFunc(
@@ -398,6 +399,55 @@ SOFTWARE.
398399
'json', 'xml', 'yaml'
399400
];
400401

402+
JUSTC.VFS = isBrowser ? class VirtualFileSystem {
403+
constructor() {
404+
this.files = new MAP();
405+
}
406+
407+
createFile(filename, content, options = {}) {
408+
const {
409+
mimeType = 'text/plain',
410+
language = 'text',
411+
syntaxHighlighting = true
412+
} = options;
413+
414+
this.files.set(filename, { content, mimeType, language });
415+
416+
if (syntaxHighlighting) {
417+
this._registerFileInDevTools(filename, content, language);
418+
}
419+
420+
return filename;
421+
}
422+
423+
_registerFileInDevTools(filename, content, language) {
424+
const blob = new BLOB([content], { type: 'text/plain' });
425+
const url = __URL__.createObjectURL(blob);
426+
427+
const script = DOCUMENT.createElement('script');
428+
script.textContent = `//# sourceMappingURL=data:application/json;base64,${btoa(JSON.stringify({
429+
version: 3,
430+
file: filename,
431+
sources: [filename],
432+
sourcesContent: [content],
433+
mappings: ""
434+
}))}`;
435+
436+
DOCUMENT.head.appendChild(script);
437+
DOCUMENT.head.removeChild(script);
438+
__URL__.revokeObjectURL(url);
439+
}
440+
441+
getFile(filename) {
442+
return this.files.get(filename);
443+
}
444+
445+
listFiles() {
446+
return ARRAY.from(this.files.keys());
447+
}
448+
} : undefined;
449+
JUSTC.CurrentVFS = isBrowser ? new JUSTC.VFS() : undefined;
450+
401451
JUSTC.Check = (code) => {
402452
if (JUSTC.CheckInput(code)) throw new JUSTC.Error(JUSTC.Errors.wrongInputType);
403453
JUSTC.CheckWASM();
@@ -435,6 +485,15 @@ SOFTWARE.
435485
};
436486
return await Promise.all(await Promise.all(JUSTC.Taskify(bool, outputMode, args.slice(start, end))))
437487
};
488+
JUSTC.RegisterImports = function(imports) {
489+
if (isBrowser && imports && ARRAY.isArray(imports) && imports.length > 0) setTimeout(() => {
490+
try {
491+
for (const [url, content, type] of imports) {
492+
JUSTC.CurrentVFS.createFile(url, content, {mimeType: type});
493+
}
494+
} catch (_) {}
495+
}, 0);
496+
};
438497
JUSTC.Output = {
439498
parse: isBrowser || !JUSTC.Experiments ? function(code, outputMode = JUSTC.DefaultOutputMode) {
440499
JUSTC.Check(code);
@@ -444,6 +503,7 @@ SOFTWARE.
444503
if (result.error) {
445504
throw new JUSTC.Error(result.error);
446505
} else {
506+
JUSTC.RegisterImports(result.imported);
447507
return result.return || {};
448508
}
449509
} : async function(...code) {
@@ -457,6 +517,7 @@ SOFTWARE.
457517
if (result.error) {
458518
throw new JUSTC.Error(result.error);
459519
} else {
520+
JUSTC.RegisterImports(result.imported);
460521
JUSTC.DisplayLogs(result);
461522
return result.return || {};
462523
}

0 commit comments

Comments
 (0)