Skip to content

Commit 3952fc8

Browse files
author
Sven Bergstr�m
committed
adding files
1 parent d4dbb0f commit 3952fc8

21 files changed

+6743
-0
lines changed

01 cli script/01 cli script.cpp

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
2+
3+
/*
4+
5+
01 - Command line script execution
6+
7+
A simple primer to understanding the v8 workflow. This is not meant
8+
to do much more than print a result of a script execution. But, it
9+
does demonstrate how to create a global template, a context, and
10+
how the script file makes it's way from disk into the context.
11+
12+
The scope of tutorial 1 is not to dig into those details, but a method
13+
of getting v8 running within minutes for you to mess around with.
14+
15+
Common:
16+
What?
17+
A simple introduction to understanding the v8 javascript engine.
18+
Why?
19+
Written for the #altdevblog www.altdevblogaday.com group.
20+
Who?
21+
Written by Sven Bergström ( FuzzYspo0N ).
22+
23+
24+
Discussion :
25+
26+
These examples will serve the simple purpose of demonstrating commonly
27+
asked questions with regards to v8 and the embedding of javascript
28+
into a c++ application.
29+
30+
Disclaimer - I am no expert on v8. I just use it, and have hit the wall
31+
a number of times. Hopefully, anything I say will help you understand the
32+
mentality, concepts and structure of the v8 engine and how to use it based
33+
on the journey that I have travelled with the engine.
34+
35+
For the full post and more info - see the following link -
36+
37+
38+
*/
39+
40+
#include <iostream>
41+
#include "../v8/v8.h"
42+
#include "../common/common.h"
43+
using namespace v8;
44+
45+
46+
int main(int argc, char **argv)
47+
{
48+
//Quite simple, these are scope based 'managers'.
49+
//One protects threading issues, and one manages
50+
//the creation of JS handles, for clean up.
51+
52+
Locker locker;
53+
HandleScope handle_scope;
54+
55+
//Create a global object template. This can be stamped
56+
//repeatedly with little cost into new execution contexts.
57+
//Note the word template! It is a 'definition' of the context.
58+
//This is important to get - when you expose something into the
59+
//global object - it is an instance. When you expose something
60+
//onto the global template - ALL contexts can share the same
61+
//exposed values without needing to redo the expose process.
62+
63+
Handle<ObjectTemplate> globalTemplate = ObjectTemplate::New();
64+
65+
//A context is a fresh execution context. Take for example,
66+
//a browser tab or frame in Google Chrome. Each of these are
67+
//a new context, stamped with a global template, and created
68+
//on demand. When executing scripts, you can execute them in
69+
//and existing context by using the Context::context_scope
70+
//handlers. This 'switches' context for that execution period
71+
72+
//In this case, we only need one new context.
73+
Handle<Context> context = Context::New( NULL, globalTemplate );
74+
75+
//If we receive an argument, execute the script file
76+
77+
if(argc > 1) {
78+
79+
eScriptExecResult r = executeScript( std::string( argv[1] ) , context );
80+
81+
} else {
82+
83+
printf("Usage: <scriptname.js> \n Execute the javascript file.");
84+
85+
}
86+
87+
return 0;
88+
}

01 cli script/01 cli script.vcproj

Lines changed: 211 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,211 @@
1+
<?xml version="1.0" encoding="Windows-1252"?>
2+
<VisualStudioProject
3+
ProjectType="Visual C++"
4+
Version="9.00"
5+
Name="01 cli script"
6+
ProjectGUID="{3CC2EC44-62A5-4E63-B8C9-E5149563DF0D}"
7+
RootNamespace="v8example"
8+
Keyword="Win32Proj"
9+
TargetFrameworkVersion="196613"
10+
>
11+
<Platforms>
12+
<Platform
13+
Name="Win32"
14+
/>
15+
</Platforms>
16+
<ToolFiles>
17+
</ToolFiles>
18+
<Configurations>
19+
<Configuration
20+
Name="Debug|Win32"
21+
OutputDirectory="$(SolutionDir)$(ConfigurationName)"
22+
IntermediateDirectory="$(ConfigurationName)"
23+
ConfigurationType="1"
24+
CharacterSet="1"
25+
>
26+
<Tool
27+
Name="VCPreBuildEventTool"
28+
/>
29+
<Tool
30+
Name="VCCustomBuildTool"
31+
/>
32+
<Tool
33+
Name="VCXMLDataGeneratorTool"
34+
/>
35+
<Tool
36+
Name="VCWebServiceProxyGeneratorTool"
37+
/>
38+
<Tool
39+
Name="VCMIDLTool"
40+
/>
41+
<Tool
42+
Name="VCCLCompilerTool"
43+
Optimization="0"
44+
PreprocessorDefinitions="WIN32;_DEBUG;_CONSOLE;_CRT_SECURE_NO_WARNINGS"
45+
MinimalRebuild="true"
46+
BasicRuntimeChecks="3"
47+
RuntimeLibrary="1"
48+
UsePrecompiledHeader="0"
49+
WarningLevel="3"
50+
DebugInformationFormat="3"
51+
/>
52+
<Tool
53+
Name="VCManagedResourceCompilerTool"
54+
/>
55+
<Tool
56+
Name="VCResourceCompilerTool"
57+
/>
58+
<Tool
59+
Name="VCPreLinkEventTool"
60+
/>
61+
<Tool
62+
Name="VCLinkerTool"
63+
AdditionalDependencies="v8.lib WS2_32.lib Winmm.lib "
64+
OutputFile="../00 bin/$(ProjectName).exe"
65+
LinkIncremental="1"
66+
AdditionalLibraryDirectories="../v8/"
67+
GenerateDebugInformation="true"
68+
ProgramDatabaseFile="$(IntDir)$(TargetName).pdb"
69+
SubSystem="1"
70+
TargetMachine="1"
71+
/>
72+
<Tool
73+
Name="VCALinkTool"
74+
/>
75+
<Tool
76+
Name="VCManifestTool"
77+
/>
78+
<Tool
79+
Name="VCXDCMakeTool"
80+
/>
81+
<Tool
82+
Name="VCBscMakeTool"
83+
/>
84+
<Tool
85+
Name="VCFxCopTool"
86+
/>
87+
<Tool
88+
Name="VCAppVerifierTool"
89+
/>
90+
<Tool
91+
Name="VCPostBuildEventTool"
92+
/>
93+
</Configuration>
94+
<Configuration
95+
Name="Release|Win32"
96+
OutputDirectory="$(SolutionDir)$(ConfigurationName)"
97+
IntermediateDirectory="$(ConfigurationName)"
98+
ConfigurationType="1"
99+
CharacterSet="1"
100+
WholeProgramOptimization="1"
101+
>
102+
<Tool
103+
Name="VCPreBuildEventTool"
104+
/>
105+
<Tool
106+
Name="VCCustomBuildTool"
107+
/>
108+
<Tool
109+
Name="VCXMLDataGeneratorTool"
110+
/>
111+
<Tool
112+
Name="VCWebServiceProxyGeneratorTool"
113+
/>
114+
<Tool
115+
Name="VCMIDLTool"
116+
/>
117+
<Tool
118+
Name="VCCLCompilerTool"
119+
Optimization="2"
120+
EnableIntrinsicFunctions="true"
121+
PreprocessorDefinitions="WIN32;NDEBUG;_CONSOLE;_CRT_SECURE_NO_WARNINGS"
122+
RuntimeLibrary="0"
123+
EnableFunctionLevelLinking="true"
124+
UsePrecompiledHeader="0"
125+
WarningLevel="3"
126+
DebugInformationFormat="3"
127+
/>
128+
<Tool
129+
Name="VCManagedResourceCompilerTool"
130+
/>
131+
<Tool
132+
Name="VCResourceCompilerTool"
133+
/>
134+
<Tool
135+
Name="VCPreLinkEventTool"
136+
/>
137+
<Tool
138+
Name="VCLinkerTool"
139+
AdditionalDependencies="v8.lib WS2_32.lib Winmm.lib "
140+
OutputFile="../00 bin/$(ProjectName).exe"
141+
LinkIncremental="1"
142+
AdditionalLibraryDirectories="../v8/"
143+
GenerateDebugInformation="true"
144+
ProgramDatabaseFile="$(IntDir)$(TargetName).pdb"
145+
SubSystem="1"
146+
OptimizeReferences="2"
147+
EnableCOMDATFolding="2"
148+
TargetMachine="1"
149+
/>
150+
<Tool
151+
Name="VCALinkTool"
152+
/>
153+
<Tool
154+
Name="VCManifestTool"
155+
/>
156+
<Tool
157+
Name="VCXDCMakeTool"
158+
/>
159+
<Tool
160+
Name="VCBscMakeTool"
161+
/>
162+
<Tool
163+
Name="VCFxCopTool"
164+
/>
165+
<Tool
166+
Name="VCAppVerifierTool"
167+
/>
168+
<Tool
169+
Name="VCPostBuildEventTool"
170+
/>
171+
</Configuration>
172+
</Configurations>
173+
<References>
174+
</References>
175+
<Files>
176+
<Filter
177+
Name="v8"
178+
>
179+
<File
180+
RelativePath="..\v8\v8-debug.h"
181+
>
182+
</File>
183+
<File
184+
RelativePath="..\v8\v8-preparser.h"
185+
>
186+
</File>
187+
<File
188+
RelativePath="..\v8\v8-profiler.h"
189+
>
190+
</File>
191+
<File
192+
RelativePath="..\v8\v8-testing.h"
193+
>
194+
</File>
195+
<File
196+
RelativePath="..\v8\v8.h"
197+
>
198+
</File>
199+
<File
200+
RelativePath="..\v8\v8stdint.h"
201+
>
202+
</File>
203+
</Filter>
204+
<File
205+
RelativePath=".\01 cli script.cpp"
206+
>
207+
</File>
208+
</Files>
209+
<Globals>
210+
</Globals>
211+
</VisualStudioProject>

0 commit comments

Comments
 (0)