FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
daScript/src/builtin/module_file_access.cpp at master · WhyNot135/daScript · GitHub
WhyNot135
/
daScript
Public
forked from
GaijinEntertainment/daScript
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
daScript
/
src
/
builtin
/
module_file_access.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
218 lines (200 loc) · 10.3 KB
Breadcrumbs
daScript
/
src
/
builtin
/
module_file_access.cpp
Copy path
File metadata and controls
218 lines (200 loc) · 10.3 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#
include
"
daScript/misc/platform.h
"
#
include
"
daScript/ast/ast.h
"
#
include
"
daScript/simulate/debug_info.h
"
#
if
!defined(DAS_NO_FILEIO)
#
include
<
sys/stat.h
>
#
endif
das::Context *
get_context
(
int
stackSize=
0
);
namespace
das
{
ModuleFileAccess::ModuleFileAccess
() {
}
ModuleFileAccess::~ModuleFileAccess
() {
if
(context)
delete
context;
}
ModuleFileAccess::ModuleFileAccess
(
const
string & pak,
const
ProgramPtr & program ) {
TextWriter tout;
if
( program ) {
if
( program->
failed
() ) {
for
(
auto
& err : program->
errors
) {
tout <<
reportError
(err.
at
, err.
what
, err.
extra
, err.
fixme
, err.
cerr
);
}
DAS_FATAL_ERROR
(
"
failed to compile: %s
\n
%s
\n
"
, pak.
c_str
(), tout.
str
().
c_str
());
}
else
{
context =
get_context
(program->
getContextStackSize
());
if
( !program->
simulate
(*context, tout) ) {
tout <<
"
failed to simulate
\n
"
;
for
(
auto
& err : program->
errors
) {
tout <<
reportError
(err.
at
, err.
what
, err.
extra
, err.
fixme
, err.
cerr
);
}
delete
context;
context =
nullptr
;
DAS_FATAL_ERROR
(
"
failed to simulate: %s
\n
%s
"
, pak.
c_str
(), tout.
str
().
c_str
());
return
;
}
modGet = context->
findFunction
(
"
module_get
"
);
DAS_ASSERTF
(modGet!=
nullptr
,
"
can't find module_get function
"
);
includeGet = context->
findFunction
(
"
include_get
"
);
//
note, this one CAN be null
moduleAllowed = context->
findFunction
(
"
module_allowed
"
);
//
note, this one CAN be null
moduleUnsafe = context->
findFunction
(
"
module_allowed_unsafe
"
);
//
note, this one CAN be null
canModuleBeRequired = context->
findFunction
(
"
can_module_be_required
"
);
//
note, this one CAN be null
sameFileName = context->
findFunction
(
"
is_same_file_name
"
);
//
note, this one CAN be null
optionAllowed = context->
findFunction
(
"
option_allowed
"
);
//
note, this one CAN be null
optionBlocked = context->
findFunction
(
"
option_blocked
"
);
//
note, this one CAN be null
annotationAllowed = context->
findFunction
(
"
annotation_allowed
"
);
//
note, this one CAN be null
podInScopeAllowed = context->
findFunction
(
"
is_pod_in_scope_allowed
"
);
//
note, this one CAN be null
dynModulesFolderGet = context->
findFunction
(
"
dyn_modules_folder
"
);
//
note, this one CAN be null
//
get it ready
context->
restart
();
context->
runInitScript
();
//
note: we assume sane init stack size here
//
setup pak root
int
ipr = context->
findVariable
(
"
DAS_PAK_ROOT
"
);
if
( ipr != -
1
) {
//
TODO: verify if its string
string pakRoot =
"
.
"
;
auto
np = pak.
find_last_of
(
"
\\
/
"
);
if
( np != string::npos ) {
pakRoot = pak.
substr
(
0
,np+
1
);
}
//
set it
char
** gpr = (
char
**) context->
getVariable
(ipr);
*gpr = context->
allocateString
(pakRoot,
/*
at
*/
nullptr
);
}
//
logs
auto
tostr = tout.
str
();
if
( !tostr.
empty
() ) {
printf
(
"
%s
\n
"
, tostr.
c_str
());
}
}
}
else
{
DAS_FATAL_ERROR
(
"
failed to compile: %s
\n
%s
"
, pak.
c_str
(), tout.
str
().
c_str
());
}
}
int64_t
FileAccess::getFileMtime
(
const
string & fileName)
const
{
#
if
!defined(DAS_NO_FILEIO)
struct
stat
st;
stat
(fileName.
c_str
(), &st);
return
st.
st_mtime
;
#
else
return
-
1
;
#
endif
}
bool
ModuleFileAccess::canModuleBeUnsafe
(
const
string & mod,
const
string & fileName )
const
{
if
(
failed
() || !moduleUnsafe)
return
FileAccess::canModuleBeUnsafe
(mod,fileName);
vec4f args[
2
];
args[
0
] = cast<
const
char
*>::
from
(mod.
c_str
());
args[
1
] = cast<
const
char
*>::
from
(fileName.
c_str
());
auto
res = context->
evalWithCatch
(moduleUnsafe, args,
nullptr
);
auto
exc = context->
getException
(); exc;
DAS_ASSERTF
(!exc,
"
exception failed in `module_unsafe`: %s
"
, exc);
return
cast<
bool
>::
to
(res);
}
bool
ModuleFileAccess::canBeRequired
(
const
string & mod,
const
string & fileName,
bool
isPublic )
const
{
if
(
failed
() || !canModuleBeRequired)
return
FileAccess::canBeRequired
(mod,fileName,isPublic);
vec4f args[
3
];
args[
0
] = cast<
const
char
*>::
from
(mod.
c_str
());
args[
1
] = cast<
const
char
*>::
from
(fileName.
c_str
());
args[
2
] = cast<
bool
>::
from
(isPublic);
auto
res = context->
evalWithCatch
(canModuleBeRequired, args,
nullptr
);
auto
exc = context->
getException
(); exc;
DAS_ASSERTF
(!exc,
"
exception failed in `can_module_be_required`: %s
"
, exc);
return
cast<
bool
>::
to
(res);
}
bool
ModuleFileAccess::isModuleAllowed
(
const
string & mod,
const
string & fileName )
const
{
if
(
failed
() || !moduleAllowed)
return
FileAccess::isModuleAllowed
(mod,fileName);
vec4f args[
2
];
args[
0
] = cast<
const
char
*>::
from
(mod.
c_str
());
args[
1
] = cast<
const
char
*>::
from
(fileName.
c_str
());
auto
res = context->
evalWithCatch
(moduleAllowed, args,
nullptr
);
auto
exc = context->
getException
(); exc;
DAS_ASSERTF
(!exc,
"
exception failed in `module_allowed`: %s
"
, exc);
return
cast<
bool
>::
to
(res);
}
ModuleInfo
ModuleFileAccess::getModuleInfo
(
const
string & req,
const
string & from )
const
{
if
(
failed
())
return
FileAccess::getModuleInfo
(req, from);
vec4f args[
2
];
args[
0
] = cast<
const
char
*>::
from
(req.
c_str
());
args[
1
] = cast<
const
char
*>::
from
(from.
c_str
());
struct
{
char
* modName;
char
* modFileName;
char
* modImportName;
} res;
memset
(&res,
0
,
sizeof
(res));
context->
evalWithCatch
(modGet, args, &res);
auto
exc = context->
getException
(); exc;
DAS_ASSERTF
(!exc,
"
exception failed in `module_get`: %s
"
, exc);
ModuleInfo info;
info.
moduleName
= res.
modName
? res.
modName
:
"
"
;
info.
fileName
= res.
modFileName
? res.
modFileName
:
"
"
;
info.
importName
= res.
modImportName
? res.
modImportName
:
"
"
;
return
info;
}
string
ModuleFileAccess::getDynModulesFolder
()
const
{
if
(
failed
() || !dynModulesFolderGet)
return
FileAccess::getDynModulesFolder
();
auto
res = context->
evalWithCatch
(dynModulesFolderGet,
nullptr
,
nullptr
);
auto
exc = context->
getException
(); exc;
DAS_ASSERTF
(!exc,
"
exception failed in `dyn_modules_folder`: %s
"
, exc);
return
cast<
const
char
*>::
to
(res);
}
string
ModuleFileAccess::getIncludeFileName
(
const
string & fileName,
const
string & incFileName )
const
{
if
(
failed
() || !includeGet)
return
FileAccess::getIncludeFileName
(fileName,incFileName);
vec4f args[
2
];
args[
0
] = cast<
const
char
*>::
from
(incFileName.
c_str
());
args[
1
] = cast<
const
char
*>::
from
(fileName.
c_str
());
vec4f res = context->
evalWithCatch
(includeGet, args,
nullptr
);
auto
exc = context->
getException
(); exc;
DAS_ASSERTF
(!exc,
"
exception failed in `include_get`: %s
"
, exc);
auto
fname = cast<
const
char
*>::
to
(res);
return
fname ? fname :
"
"
;
}
bool
ModuleFileAccess::isSameFileName
(
const
string & a,
const
string & b )
const
{
if
(
failed
() || !sameFileName)
return
FileAccess::isSameFileName
(a,b);
vec4f args[
2
];
args[
0
] = cast<
const
char
*>::
from
(a.
c_str
());
args[
1
] = cast<
const
char
*>::
from
(b.
c_str
());
vec4f res = context->
evalWithCatch
(sameFileName, args,
nullptr
);
auto
exc = context->
getException
(); exc;
DAS_ASSERTF
(!exc,
"
exception failed in `is_same_file_name`: %s
"
, exc);
return
cast<
bool
>::
to
(res);
}
bool
ModuleFileAccess::isOptionAllowed
(
const
string & opt,
const
string & from )
const
{
if
(
failed
() || !optionAllowed)
return
FileAccess::isOptionAllowed
(opt,from);
vec4f args[
2
];
args[
0
] = cast<
const
char
*>::
from
(opt.
c_str
());
args[
1
] = cast<
const
char
*>::
from
(from.
c_str
());
vec4f res = context->
evalWithCatch
(optionAllowed, args,
nullptr
);
auto
exc = context->
getException
(); exc;
DAS_ASSERTF
(!exc,
"
exception failed in `option_allowed`: %s
"
, exc);
return
cast<
bool
>::
to
(res);
}
bool
ModuleFileAccess::isOptionBlocked
(
const
string & opt,
const
string & from )
const
{
if
(
failed
() || !optionBlocked)
return
FileAccess::isOptionBlocked
(opt,from);
vec4f args[
2
];
args[
0
] = cast<
const
char
*>::
from
(opt.
c_str
());
args[
1
] = cast<
const
char
*>::
from
(from.
c_str
());
vec4f res = context->
evalWithCatch
(optionBlocked, args,
nullptr
);
auto
exc = context->
getException
(); exc;
DAS_ASSERTF
(!exc,
"
exception failed in `option_blocked`: %s
"
, exc);
return
cast<
bool
>::
to
(res);
}
bool
ModuleFileAccess::isAnnotationAllowed
(
const
string & ann,
const
string & from )
const
{
if
(
failed
() || !annotationAllowed)
return
FileAccess::isAnnotationAllowed
(ann,from);
vec4f args[
2
];
args[
0
] = cast<
const
char
*>::
from
(ann.
c_str
());
args[
1
] = cast<
const
char
*>::
from
(from.
c_str
());
vec4f res = context->
evalWithCatch
(annotationAllowed, args,
nullptr
);
auto
exc = context->
getException
(); exc;
DAS_ASSERTF
(!exc,
"
exception failed in `annotation_allowed`: %s
"
, exc);
return
cast<
bool
>::
to
(res);
}
bool
ModuleFileAccess::isPodInScopeAllowed
(
const
string & moduleName,
const
string & fileName )
const
{
if
(
failed
() || !podInScopeAllowed)
return
FileAccess::isPodInScopeAllowed
(moduleName,fileName);
vec4f args[
2
];
args[
0
] = cast<
const
char
*>::
from
(moduleName.
c_str
());
args[
1
] = cast<
const
char
*>::
from
(fileName.
c_str
());
vec4f res = context->
evalWithCatch
(podInScopeAllowed, args,
nullptr
);
auto
exc = context->
getException
(); exc;
DAS_ASSERTF
(!exc,
"
exception failed in `pod_in_scope_allowed`: %s
"
, exc);
return
cast<
bool
>::
to
(res);
}
}
Back
|
FazBrowse Home
|
New Git URL