Skip to content

Commit 52c0098

Browse files
author
marja@chromium.org
committed
New Compilation API, part 1
- Distinguish between context bound scripts (Script) and context unbound scripts (UnboundScript). - Add ScriptCompiler (which will later contain functions for async compilation). This is a breaking change, in particular, Script::New no longer exists (it is replaced by ScriptCompiler::CompileUnbound). Script::Compile remains as a backwards-compatible shorthand for ScriptCompiler::Compile. Passing CompilerOptions with produce_data_to_cache = true doesn't do anything yet; the only way to generate the data to cache is the old preparsing API. (To be fixed in the next version.) BUG= R=dcarney@chromium.org Review URL: https://codereview.chromium.org/186723005 git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@19881 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
1 parent 750ab88 commit 52c0098

14 files changed

Lines changed: 418 additions & 277 deletions

File tree

include/v8.h

Lines changed: 121 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ class ObjectTemplate;
108108
class Platform;
109109
class Primitive;
110110
class RawOperationDescriptor;
111+
class Script;
111112
class Signature;
112113
class StackFrame;
113114
class StackTrace;
@@ -1146,95 +1147,153 @@ class ScriptOrigin {
11461147

11471148

11481149
/**
1149-
* A compiled JavaScript script.
1150+
* A compiled JavaScript script, not yet tied to a Context.
11501151
*/
1151-
class V8_EXPORT Script {
1152+
class V8_EXPORT UnboundScript {
11521153
public:
11531154
/**
1154-
* Compiles the specified script (context-independent).
1155-
*
1156-
* \param source Script source code.
1157-
* \param origin Script origin, owned by caller, no references are kept
1158-
* when New() returns
1159-
* \param pre_data Pre-parsing data, as obtained by ScriptData::PreCompile()
1160-
* using pre_data speeds compilation if it's done multiple times.
1161-
* Owned by caller, no references are kept when New() returns.
1162-
* \return Compiled script object (context independent; when run it
1163-
* will use the currently entered context).
1155+
* Binds the script to the currently entered context.
11641156
*/
1165-
static Local<Script> New(Handle<String> source,
1166-
ScriptOrigin* origin = NULL,
1167-
ScriptData* pre_data = NULL);
1157+
Local<Script> BindToCurrentContext();
1158+
1159+
int GetId();
1160+
Handle<Value> GetScriptName();
11681161

11691162
/**
1170-
* Compiles the specified script using the specified file name
1171-
* object (typically a string) as the script's origin.
1172-
*
1173-
* \param source Script source code.
1174-
* \param file_name file name object (typically a string) to be used
1175-
* as the script's origin.
1176-
* \return Compiled script object (context independent; when run it
1177-
* will use the currently entered context).
1163+
* Returns zero based line number of the code_pos location in the script.
1164+
* -1 will be returned if no information available.
11781165
*/
1179-
static Local<Script> New(Handle<String> source,
1180-
Handle<Value> file_name);
1166+
int GetLineNumber(int code_pos);
1167+
1168+
static const int kNoScriptId = 0;
1169+
};
1170+
11811171

1172+
/**
1173+
* A compiled JavaScript script, tied to a Context which was active when the
1174+
* script was compiled.
1175+
*/
1176+
class V8_EXPORT Script {
1177+
public:
11821178
/**
1183-
* Compiles the specified script (bound to current context).
1184-
*
1185-
* \param source Script source code.
1186-
* \param origin Script origin, owned by caller, no references are kept
1187-
* when Compile() returns
1188-
* \param pre_data Pre-parsing data, as obtained by ScriptData::PreCompile()
1189-
* using pre_data speeds compilation if it's done multiple times.
1190-
* Owned by caller, no references are kept when Compile() returns.
1191-
* \return Compiled script object, bound to the context that was active
1192-
* when this function was called. When run it will always use this
1193-
* context.
1179+
* A shorthand for ScriptCompiler::CompileBound().
11941180
*/
11951181
static Local<Script> Compile(Handle<String> source,
1196-
ScriptOrigin* origin = NULL,
1197-
ScriptData* pre_data = NULL);
1182+
ScriptOrigin* origin = NULL);
11981183

1199-
/**
1200-
* Compiles the specified script using the specified file name
1201-
* object (typically a string) as the script's origin.
1202-
*
1203-
* \param source Script source code.
1204-
* \param file_name File name to use as script's origin
1205-
* \return Compiled script object, bound to the context that was active
1206-
* when this function was called. When run it will always use this
1207-
* context.
1208-
*/
1184+
// To be decprecated, use the Compile above.
12091185
static Local<Script> Compile(Handle<String> source,
1210-
Handle<Value> file_name);
1186+
Handle<String> file_name);
12111187

12121188
/**
1213-
* Runs the script returning the resulting value. If the script is
1214-
* context independent (created using ::New) it will be run in the
1215-
* currently entered context. If it is context specific (created
1216-
* using ::Compile) it will be run in the context in which it was
1217-
* compiled.
1189+
* Runs the script returning the resulting value. It will be run in the
1190+
* context in which it was created (ScriptCompiler::CompileBound or
1191+
* UnboundScript::BindToGlobalContext()).
12181192
*/
12191193
Local<Value> Run();
12201194

12211195
/**
1222-
* Returns the script id.
1196+
* Returns the corresponding context-unbound script.
12231197
*/
1224-
int GetId();
1198+
Local<UnboundScript> GetUnboundScript();
12251199

1226-
/**
1227-
* Returns the name value of one Script.
1228-
*/
1229-
Handle<Value> GetScriptName();
1200+
// To be deprecated; use GetUnboundScript()->GetId();
1201+
int GetId() {
1202+
return GetUnboundScript()->GetId();
1203+
}
1204+
1205+
// Use GetUnboundScript()->GetId();
1206+
V8_DEPRECATED("Use GetUnboundScript()->GetId()",
1207+
Handle<Value> GetScriptName()) {
1208+
return GetUnboundScript()->GetScriptName();
1209+
}
12301210

12311211
/**
12321212
* Returns zero based line number of the code_pos location in the script.
12331213
* -1 will be returned if no information available.
12341214
*/
1235-
int GetLineNumber(int code_pos);
1215+
V8_DEPRECATED("Use GetUnboundScript()->GetLineNumber()",
1216+
int GetLineNumber(int code_pos)) {
1217+
return GetUnboundScript()->GetLineNumber(code_pos);
1218+
}
1219+
};
12361220

1237-
static const int kNoScriptId = 0;
1221+
1222+
/**
1223+
* For compiling scripts.
1224+
*/
1225+
class V8_EXPORT ScriptCompiler {
1226+
public:
1227+
/**
1228+
* Compilation data that the embedder can cache and pass back to speed up
1229+
* future compilations. The data is produced if the CompilerOptions passed to
1230+
* the compilation functions in ScriptCompiler contains produce_data_to_cache
1231+
* = true. The data to cache can then can be retrieved from
1232+
* UnboundScript.
1233+
*/
1234+
struct V8_EXPORT CachedData {
1235+
CachedData() : data(NULL), length(0) {}
1236+
// Caller keeps the ownership of data and guarantees that the data stays
1237+
// alive long enough.
1238+
CachedData(const uint8_t* data, int length) : data(data), length(length) {}
1239+
// TODO(marja): Async compilation; add constructors which take a callback
1240+
// which will be called when V8 no longer needs the data.
1241+
const uint8_t* data;
1242+
int length;
1243+
};
1244+
1245+
/**
1246+
* Source code which can be then compiled to a UnboundScript or
1247+
* BoundScript.
1248+
*/
1249+
struct V8_EXPORT Source {
1250+
Source(Local<String> source_string, const ScriptOrigin& origin,
1251+
const CachedData& cached_data = CachedData());
1252+
Source(Local<String> source_string,
1253+
const CachedData& cached_data = CachedData());
1254+
1255+
Local<String> source_string;
1256+
1257+
// Origin information
1258+
Handle<Value> resource_name;
1259+
Handle<Integer> resource_line_offset;
1260+
Handle<Integer> resource_column_offset;
1261+
Handle<Boolean> resource_is_shared_cross_origin;
1262+
1263+
// Cached data from previous compilation (if any).
1264+
CachedData cached_data;
1265+
};
1266+
1267+
enum CompileOptions {
1268+
kNoCompileOptions,
1269+
kProduceDataToCache = 1 << 0
1270+
};
1271+
1272+
/**
1273+
* Compiles the specified script (context-independent).
1274+
*
1275+
* \param source Script source code.
1276+
* \return Compiled script object (context independent; for running it must be
1277+
* bound to a context).
1278+
*/
1279+
static Local<UnboundScript> CompileUnbound(
1280+
Isolate* isolate, const Source& source,
1281+
CompileOptions options = kNoCompileOptions);
1282+
1283+
/**
1284+
* Compiles the specified script (bound to current context).
1285+
*
1286+
* \param source Script source code.
1287+
* \param pre_data Pre-parsing data, as obtained by ScriptData::PreCompile()
1288+
* using pre_data speeds compilation if it's done multiple times.
1289+
* Owned by caller, no references are kept when this function returns.
1290+
* \return Compiled script object, bound to the context that was active
1291+
* when this function was called. When run it will always use this
1292+
* context.
1293+
*/
1294+
static Local<Script> Compile(
1295+
Isolate* isolate, const Source& source,
1296+
CompileOptions options = kNoCompileOptions);
12381297
};
12391298

12401299

samples/lineprocessor.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,8 @@ int RunMain(int argc, char* argv[]) {
238238
{
239239
// Compile script in try/catch context.
240240
v8::TryCatch try_catch;
241-
script = v8::Script::Compile(script_source, script_name);
241+
v8::ScriptOrigin origin(script_name);
242+
script = v8::Script::Compile(script_source, &origin);
242243
if (script.IsEmpty()) {
243244
// Print errors that happened during compilation.
244245
if (report_exceptions)

samples/shell.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,8 @@ bool ExecuteString(v8::Isolate* isolate,
304304
bool report_exceptions) {
305305
v8::HandleScope handle_scope(isolate);
306306
v8::TryCatch try_catch;
307-
v8::Handle<v8::Script> script = v8::Script::Compile(source, name);
307+
v8::ScriptOrigin origin(name);
308+
v8::Handle<v8::Script> script = v8::Script::Compile(source, &origin);
308309
if (script.IsEmpty()) {
309310
// Print errors that happened during compilation.
310311
if (report_exceptions)

0 commit comments

Comments
 (0)