forked from gfwilliams/tiny-js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodules.cpp
More file actions
101 lines (85 loc) · 2.26 KB
/
modules.cpp
File metadata and controls
101 lines (85 loc) · 2.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*
* File: modules.cpp
* Author: ghernan
*
* Module system for AsyncScript
*
* Created on May 15, 2017, 11:09 PM
*/
#include "ascript_pch.hpp"
#include "modules.h"
#include "utils.h"
#include "microVM.h"
#include "scriptMain.h"
#include "ScriptException.h"
using namespace std;
/**
* Normalizes a module path.
* @param modulePath
* @param ec
* @return
*/
std::string normalizeModulePath (const std::string& modulePath, ExecutionContext *ec)
{
string result;
if (isPathRelative(modulePath))
{
string base = dirFromPath( ec->modulePath );
result = joinPaths (base, modulePath);
}
else
result = modulePath;
return normalizePath (result);
}
/**
* Tries to find a module into the currently loaded module list.
* Returns null if not found.
* @param modulePath
* @param ec
* @return
*/
ASValue findModule (const std::string& modulePath, ExecutionContext *ec)
{
ASSERT (ec->modules != NULL);
auto it = ec->modules->modules.find(modulePath);
if (it != ec->modules->modules.end())
return it->second;
else
return jsNull();
}
/**
* Loads a new module, from its path in the file system.
*
* @param modulePath
* @param ec
* @return
*/
ASValue loadModule (const std::string& modulePath, ExecutionContext *ec)
{
string script = readTextFile(modulePath);
if (script.empty())
rtError ("Module '%s' not found", modulePath.c_str());
auto globals = createDefaultGlobals();
ec->modules->modules[modulePath] = globals->value();
evaluate(script.c_str(), globals, modulePath, ec);
return globals->value();
}
/**
* Mix a module contents into a target module
* @param target
* @param source
* @param ec
*/
void mixModule (ASValue target, ASValue source, ExecutionContext *ec)
{
ASSERT (target.getType() == VT_OBJECT);
ASSERT (source.getType() == VT_OBJECT);
auto tgtObj = target.staticCast<JSObject>();
auto srcObj = source.staticCast<JSObject>();
StringSet srcFields = srcObj->getFields(false);
for (auto it = srcFields.begin(); it != srcFields.end(); ++it)
{
if (srcObj->getFieldProperty(*it, "export").toBoolean(ec) == true)
tgtObj->writeField(*it, srcObj->readField(*it), true);
}
}