From c75421ee43f22df8c2405302e6118548c97f13bc Mon Sep 17 00:00:00 2001 From: Roberto Raggi Date: Thu, 10 May 2012 10:56:02 +0200 Subject: [PATCH] Add the String object constructor. --- main.cpp | 26 +++++++++++++++++++++++++- qmljs_objects.h | 15 +++++++++++++++ qmljs_runtime.h | 3 +++ tests/string.1.js | 7 +++++++ 4 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 tests/string.1.js diff --git a/main.cpp b/main.cpp index 3266a35..ef5d6eb 100644 --- a/main.cpp +++ b/main.cpp @@ -48,10 +48,31 @@ struct ObjectCtor: FunctionObject __qmljs_init_object(ctx, &ctx->result, new Object()); } - virtual void call(Context *) { + virtual void call(Context *) + { assert(!"not here"); } }; + +struct StringCtor: FunctionObject +{ + virtual void construct(Context *ctx) + { + Value arg = ctx->argument(0); + __qmljs_to_string(ctx, &arg, &arg); + __qmljs_init_object(ctx, &ctx->thisObject, new StringObject(arg)); + } + + virtual void call(Context *ctx) + { + const Value arg = ctx->argument(0); + if (arg.is(UNDEFINED_TYPE)) + __qmljs_init_string(ctx, &ctx->result, String::get(ctx, QString())); + else + __qmljs_to_string(ctx, &ctx->result, &arg); + } +}; + } // builtins @@ -103,6 +124,9 @@ void evaluate(QQmlJS::Engine *engine, const QString &fileName, const QString &co ctx->activation.objectValue->put(VM::String::get(ctx, QLatin1String("Object")), VM::Value::object(ctx, new builtins::ObjectCtor())); + ctx->activation.objectValue->put(VM::String::get(ctx, QLatin1String("String")), + VM::Value::object(ctx, new builtins::StringCtor())); + foreach (IR::Function *function, module.functions) { if (function->name && ! function->name->isEmpty()) { ctx->activation.objectValue->put(VM::String::get(ctx, *function->name), diff --git a/qmljs_objects.h b/qmljs_objects.h index 15df292..dbb1ab8 100644 --- a/qmljs_objects.h +++ b/qmljs_objects.h @@ -277,6 +277,21 @@ struct Context { String **formals; size_t formalCount; + inline Value argument(size_t index = 0) + { + Value arg; + getArgument(&arg, index); + return arg; + } + + inline void getArgument(Value *result, size_t index) + { + if (index < argumentCount) + *result = arguments[index]; + else + __qmljs_init_undefined(this, result); + } + void init() { parent = 0; diff --git a/qmljs_runtime.h b/qmljs_runtime.h index 61d620f..c75e502 100644 --- a/qmljs_runtime.h +++ b/qmljs_runtime.h @@ -171,6 +171,9 @@ struct Value { String *stringValue; }; + inline bool is(ValueType t) const { return type == t; } + inline bool isNot(ValueType t) const { return type != t; } + static inline Value boolean(Context *ctx, bool value) { Value v; __qmljs_init_boolean(ctx, &v, value); diff --git a/tests/string.1.js b/tests/string.1.js new file mode 100644 index 0000000..1495552 --- /dev/null +++ b/tests/string.1.js @@ -0,0 +1,7 @@ + +var s = String(123) + 1 +print(s) + +var s2 = new String(123) +print(s2) + -- 2.7.4