FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
daScript/examples/tutorial/tutorial09.cpp at master · fiveakatto/daScript · GitHub
fiveakatto
/
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
/
examples
/
tutorial
/
tutorial09.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
100 lines (92 loc) · 3.62 KB
Breadcrumbs
daScript
/
examples
/
tutorial
/
tutorial09.cpp
Copy path
File metadata and controls
100 lines (92 loc) · 3.62 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
#
include
"
daScript/daScript.h
"
using
namespace
das
;
GcRootLambda rootLambda;
void
setLambda
( Lambda lmb, Context * ctx ) {
//
store lambda in global variable, which is only visible from the C++ side
printf
(
"
setting lambda from C++ side
\n
"
);
rootLambda =
GcRootLambda
(lmb,ctx);
}
//
making custom builtin moGcdule
class
Module_Tutorial09
:
public
Module
{
public:
Module_Tutorial09
() : Module(
"
tutorial_09
"
) {
//
module name, when used from das file
ModuleLibrary
lib
(
this
);
lib.
addBuiltInModule
();
//
register function 'set_lambda' with the module
addExtern<
DAS_BIND_FUN
(setLambda)>(*
this
,lib,
"
set_lambda
"
,SideEffects::modifyExternal,
"
setLambda
"
);
}
};
//
registering module, so that its available via 'NEED_MODULE' macro
REGISTER_MODULE
(Module_Tutorial09);
#
define
TUTORIAL_NAME
"
/examples/tutorial/tutorial09.das
"
void
tutorial
() {
TextPrinter tout;
//
output stream for all compiler messages (stdout. for stringstream use TextWriter)
ModuleGroup dummyLibGroup;
//
module group for compiled program
auto
fAccess
= make_smart<FsFileAccess>();
//
default file access
CodeOfPolicies policies;
//
default policies
policies.
persistent_heap
=
true
;
//
enable persistent heap for GC purposes
//
compile program
auto
program =
compileDaScript
(
getDasRoot
() +
TUTORIAL_NAME
,
fAccess
, tout, dummyLibGroup, policies);
if
( program->
failed
() ) {
//
if compilation failed, report errors
tout <<
"
failed to compile
\n
"
;
for
(
auto
& err : program->
errors
) {
tout <<
reportError
(err.
at
, err.
what
, err.
extra
, err.
fixme
, err.
cerr
);
}
return
;
}
//
create daScript context
Context
ctx
(program->
getContextStackSize
());
if
( !program->
simulate
(ctx, tout) ) {
//
if interpretation failed, report errors
tout <<
"
failed to simulate
\n
"
;
for
(
auto
& err : program->
errors
) {
tout <<
reportError
(err.
at
, err.
what
, err.
extra
, err.
fixme
, err.
cerr
);
}
return
;
}
//
find function 'test' in the context
auto
fnTest = ctx.
findFunction
(
"
test
"
);
if
( !fnTest ) {
tout <<
"
function 'test' not found
\n
"
;
return
;
}
//
verify if 'test' is a function, with the correct signature
//
note, this operation is slow, so don't do it every time for every call
if
( !verifyCall<
void
>(fnTest->
debugInfo
, dummyLibGroup) ) {
tout <<
"
function 'test', call arguments do not match. expecting def test : void
\n
"
;
return
;
}
//
evaluate 'test' function in the context
for
(
int
i=
0
; i!=
3
; ++i ) {
//
call garbage collector
ctx.
reportAnyHeap
(
nullptr
,
true
,
true
,
false
,
false
);
ctx.
collectHeap
(
nullptr
,
true
,
true
);
//
now call lambda, if its there
if
( rootLambda.
capture
) {
tout <<
"
calling lambda
\n
"
;
das_invoke_lambda<
void
>::
invoke
(&ctx,
nullptr
, rootLambda);
}
//
now done
ctx.
evalWithCatch
(fnTest,
nullptr
);
if
(
auto
ex = ctx.
getException
() ) {
//
if function cased panic, report it
tout <<
"
exception:
"
<< ex <<
"
\n
"
;
return
;
}
}
//
reset root lambda
rootLambda.
reset
();
}
int
main
(
int
,
char
* [] ) {
//
request all da-script built in modules
NEED_ALL_DEFAULT_MODULES
;
//
request our custom module
NEED_MODULE
(Module_Tutorial09);
//
Initialize modules
Module::Initialize
();
//
run the tutorial
tutorial
();
//
shut-down daScript, free all memory
Module::Shutdown
();
return
0
;
}
Back
|
FazBrowse Home
|
New Git URL