Skip to content

Commit c5200c8

Browse files
author
Michael O'Brien
committed
CLEAN
1 parent 127b236 commit c5200c8

File tree

232 files changed

+73475
-1002558
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

232 files changed

+73475
-1002558
lines changed
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
{
2+
title: 'Embedding',
3+
crumbs: [
4+
{ "Developer's Guide": "index.html" },
5+
],
6+
}
7+
<h1>Embedding Ejscript</h1>
8+
<p>It is easy to embed Ejscript into an application. Ejscript provides two levels of embedding APIs:</p>
9+
<ul>
10+
<li>One line embedding APIs</li>
11+
<li>Full control APIs</li>
12+
</ul><a id="oneLine"></a>
13+
<h2 >One Line Embedding</h2>
14+
<p>Ejscript provides three variety of one-line embedding APIs. These permit you to evaluate a script file,
15+
a script literal or a pre-compiled script module.</p><a id="file"></a>
16+
<h3>Evaluate a Script File</h3>
17+
<p>To create an application that can evaluate script files, use the <b>ejsEvalFile</b> API.</p>
18+
<pre class="ui code segment">
19+
#include "ejs/ejs.h"
20+
int main(int argc, char **argv)
21+
{
22+
<b>if (ejsEvalFile("test.es") &lt; 0) {</b>
23+
fprintf(stderr, "Error executing test.es\n");
24+
exit(1);
25+
}
26+
return 0;
27+
}
28+
</pre>
29+
<p>To build this program, you just need to compile and link with Ejscript compiler library <b>libejs</b>.
30+
Here is the command for building such a test program called <b>main.c</b> on a MAC system.</p>
31+
<pre class="ui code segment">
32+
cc -o main main.c -lejs
33+
</pre>
34+
<a id="literal"></a>
35+
<h3>Evaluate a Script Literal</h3>
36+
<p>To create an application that can evaluate script literals, use the <b>ejsEvalScript</b> API.</p>
37+
<pre class="ui code segment">
38+
#include "ejs/ejs.h"
39+
int main(int argc, char **argv)
40+
{
41+
if (argc != 2) {
42+
fprintf(stderr, "usage: main script\n");
43+
exit(1);
44+
}
45+
<b>if (ejsEvalScript(argv[1]) &lt; 0) {</b>
46+
fprintf(stderr, "Error executing %s\n", argv[1]);
47+
exit(1);
48+
}
49+
return 0;
50+
}
51+
</pre>
52+
<p>Then run this program and put a literal script on the command line.</p>
53+
<pre class="ui code segment">
54+
./main "print('Hello World')"
55+
</pre><a id="module"></a>
56+
<h3>Evaluate a Script Module</h3>
57+
<p>To create an application that can load and evaluate compiled script module files, use the
58+
<b>ejsEvalModule</b> API.</p>
59+
<pre class="ui code segment">
60+
#include "ejs/ejs.h"
61+
int main(int argc, char **argv)
62+
{
63+
<b>if (ejsEvalModule("test.mod") &lt; 0) {</b>
64+
fprintf(stderr, "Error executing test.mod\n");
65+
exit(1);
66+
}
67+
return 0;
68+
}
69+
</pre>
70+
<p>To build this program, you just need link with Ejscript VM library <b>libejs</b>. Here is the command
71+
for building such a test program called <b>main.c</b> on a MAC system.</p>
72+
<pre class="ui code segment">
73+
cc -o main main.c -lejs
74+
</pre>
75+
<a id="control"></a>
76+
<h2 >Full Control Embedding</h2>
77+
<p>In some situations, your application may need full control over the Ejscript service. Some reasons for
78+
this include:</p>
79+
<ul>
80+
<li>You need to create multiple interpreter instances</li>
81+
<li>You want custom control over where Ejscript searches for modules</li>
82+
<li>You want to customize the interpreter with native types, methods or variables</li>
83+
</ul>
84+
<p>Ejscript provides a comprehensive <a href="../ref/api/ejscript.html">Ejscript Native API</a> so you can
85+
fully control the configuration and operation of Ejscript. The APIs allow you to create and manage
86+
interpreters, create types, run methods, set and get properties and interact with all the objects held by
87+
the interpreter.</p>
88+
<p>Ejscript leverages the Embedthis Portable Runtime (MPR) which is a cross-platform set of services that
89+
abstract the operating system. The MPR enhances the security of Ejscript by providing secure,
90+
high-performance, and well proven routines for managing memory, strings, lists, files, network sockets,
91+
http and threads. MPR APIs all begin with an <b>mpr</b> prefix whereas all Ejscript APIs begin with
92+
<b>ejs</b> for the VM or <b>ejsc</b> for the compiler.</p>
93+
<p>The sample below demonstrates the full control steps to compile and evaluate a file.</p>
94+
<pre class="ui code segment">
95+
int ejsEvalFile(int fileCount, char **fileList)
96+
{
97+
Ejs *ejs; // Handle to an Ejscript interpreter
98+
int status;
99+
mprCreate(0, 0, 0);
100+
if ((ejs = ejsCreateVM(0, 0, 0)) == 0) {
101+
exit(1);
102+
}
103+
mprAddRoot(ejs);
104+
if (ejsLoadModules(ejs, 0, 0) &lt; 0) {
105+
exit(2);
106+
}
107+
if (ejsLoadScriptFile(ejs, path, NULL,
108+
EC_FLAGS_NO_OUT | EC_FLAGS_DEBUG) &lt; 0) {
109+
ejsReportError(ejs, "Error in program");
110+
exit(3);
111+
}
112+
mprDestroy(MPR_EXIT_DEFAULT);
113+
return 0;
114+
}
115+
</pre>
116+
<p>Consult the <a href="../ref/api/ejscript.html">Ejscript Native API</a> and <a href=
117+
"../ref/api/mpr.html">MPR Native API</a> for details on each API. The MPR provides a hierarchical memory
118+
allocation service, so calls to <b>mprFree</b> will free the MPR instance and everything allocated off the
119+
MPR. This includes the Ejscript service, VM, interpreter and compiler. Be sure to read the <a href=
120+
"../ref/api/mpr.html#group___mpr_mem">MPR Memory Allocation Service</a> for details.</p>
121+
<p>See the <a href="extending.html">Extending Ejscript</a> for how to interact with the interpreter,
122+
objects, types, run methods, create types and properties, delete properties, throw exceptions, cast types
123+
and more.</p>

0 commit comments

Comments
 (0)