Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
R
Root
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Admin message
为了安全,强烈建议开启2FA双因子认证:User Settings -> Account -> Enable two-factor authentication!!!
Show more breadcrumbs
cxwx
Root
Commits
a6ebfe7c
Commit
a6ebfe7c
authored
8 years ago
by
Axel Naumann
Browse files
Options
Downloads
Patches
Plain Diff
More relevant / less obscure examples. Indent.
parent
5f6eab97
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
interpreter/cling/tools/demo/cling-demo.cpp
+56
-34
56 additions, 34 deletions
interpreter/cling/tools/demo/cling-demo.cpp
with
56 additions
and
34 deletions
interpreter/cling/tools/demo/cling-demo.cpp
+
56
−
34
View file @
a6ebfe7c
...
...
@@ -9,50 +9,72 @@
#include
<cling/Interpreter/Interpreter.h>
#include
<cling/Interpreter/Value.h>
#include
<cling/Utils/Casting.h>
#include
<iostream>
#include
<string>
#include
<sstream>
///\brief Pass a value from the compiled program into cling, let cling modify it
/// and return the modified value into the compiled program.
int
setAndUpdate
(
int
arg
,
cling
::
Interpreter
&
interp
)
{
int
ret
=
arg
;
// The value that will be modified
// Update the value of ret by passing it to the interpreter.
std
::
ostringstream
sstr
;
sstr
<<
"int& ref = *(int*)"
<<
&
ret
<<
';'
;
sstr
<<
"ref = ref * ref;"
;
interp
.
process
(
sstr
.
str
());
return
ret
;
/// Definitions of declarations injected also into cling.
/// NOTE: this could also stay in a header #included here and into cling, but
/// for the sake of simplicity we just redeclare them here.
int
aGlobal
=
42
;
static
float
anotherGlobal
=
3.141
;
float
getAnotherGlobal
()
{
return
anotherGlobal
;
};
void
setAnotherGlobal
(
float
val
)
{
anotherGlobal
=
val
;
}
///\brief Call compiled functions from the interpreter.
void
useHeader
(
cling
::
Interpreter
&
interp
)
{
// We could use a header, too...
interp
.
declare
(
"int aGlobal;
\n
"
"float getAnotherGlobal();
\n
"
"void setAnotherGlobal(float val);
\n
"
);
cling
::
Value
res
;
// Will hold the result of the expression evaluation.
interp
.
process
(
"aGlobal;"
,
&
res
);
std
::
cout
<<
"aGlobal is "
<<
res
.
getAs
<
long
long
>
()
<<
'\n'
;
interp
.
process
(
"getAnotherGlobal();"
,
&
res
);
std
::
cout
<<
"getAnotherGlobal() returned "
<<
res
.
getAs
<
float
>
()
<<
'\n'
;
setAnotherGlobal
(
1.
);
// We modify the compiled value,
interp
.
process
(
"getAnotherGlobal();"
,
&
res
);
// does the interpreter see it?
std
::
cout
<<
"getAnotherGlobal() returned "
<<
res
.
getAs
<
float
>
()
<<
'\n'
;
// We modify using the interpreter, now the binary sees the new value.
interp
.
process
(
"setAnotherGlobal(7.777); getAnotherGlobal();"
);
std
::
cout
<<
"getAnotherGlobal() returned "
<<
getAnotherGlobal
()
<<
'\n'
;
}
///\brief A ridiculously complicated way of converting an int to a string.
std
::
unique_ptr
<
std
::
string
>
stringify
(
int
value
,
cling
::
Interpreter
&
interp
)
{
///\brief Call an interpreted function using its symbol address.
void
useSymbolAddress
(
cling
::
Interpreter
&
interp
)
{
// Declare a function to the interpreter. Make it extern "C" to remove
// mangling from the game.
interp
.
declare
(
"extern
\"
C
\"
int plutification(int siss, int sat) "
"{ return siss * sat; }"
);
void
*
addr
=
interp
.
getAddressOfGlobal
(
"plutification"
);
using
func_t
=
int
(
int
,
int
);
func_t
*
pFunc
=
cling
::
utils
::
VoidToFunctionPtr
<
func_t
*>
(
addr
);
std
::
cout
<<
"7 * 8 = "
<<
pFunc
(
7
,
8
)
<<
'\n'
;
}
// Declare the function to cling:
static
const
std
::
string
codeFunc
=
R"CODE(
#include <string>
std::string* createAString(const char* str) {
return new std::string(str);
})CODE"
;
interp
.
declare
(
codeFunc
);
// Call the function with "runtime" values:
std
::
ostringstream
sstr
;
sstr
<<
"createAString(
\"
"
<<
value
<<
"
\"
);"
;
cling
::
Value
res
;
// Will hold the result of the expression evaluation.
interp
.
process
(
sstr
.
str
(),
&
res
);
// Grab the return value of `createAString()`:
std
::
string
*
resStr
=
static_cast
<
std
::
string
*>
(
res
.
getPtr
());
return
std
::
unique_ptr
<
std
::
string
>
(
resStr
);
///\brief Pass a pointer into cling as a string.
void
usePointerLiteral
(
cling
::
Interpreter
&
interp
)
{
int
res
=
17
;
// The value that will be modified
// Update the value of res by passing it to the interpreter.
std
::
ostringstream
sstr
;
sstr
<<
"int& ref = *(int*)"
<<
&
res
<<
';'
;
sstr
<<
"ref = ref * ref;"
;
interp
.
process
(
sstr
.
str
());
std
::
cout
<<
"The square of 17 is "
<<
res
<<
'\n'
;
}
int
main
(
int
argc
,
const
char
*
const
*
argv
)
{
// Create the Interpreter. LLVMDIR is provided as -D during compilation.
cling
::
Interpreter
interp
(
argc
,
argv
,
LLVMDIR
);
// Create the Interpreter. LLVMDIR is provided as -D during compilation.
cling
::
Interpreter
interp
(
argc
,
argv
,
LLVMDIR
);
std
::
cout
<<
"The square of 17 is "
<<
setAndUpdate
(
17
,
interp
)
<<
'\n'
;
std
::
cout
<<
"Printing a string of 42: "
<<
*
stringify
(
42
,
interp
)
<<
'\n'
;
useHeader
(
interp
);
useSymbolAddress
(
interp
);
usePointerLiteral
(
interp
);
return
0
;
return
0
;
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment