diff options
author | Jose <jose@zeroc.com> | 2016-12-28 10:50:12 +0100 |
---|---|---|
committer | Jose <jose@zeroc.com> | 2016-12-28 10:50:12 +0100 |
commit | c7943b32df74f525013c8b7e17c92a032d74c868 (patch) | |
tree | 0ce9bc34394de9929696ab18aae84623778e3eb0 /cpp/src/slice2js | |
parent | Bumped timeout for server shutdown (diff) | |
download | ice-c7943b32df74f525013c8b7e17c92a032d74c868.tar.bz2 ice-c7943b32df74f525013c8b7e17c92a032d74c868.tar.xz ice-c7943b32df74f525013c8b7e17c92a032d74c868.zip |
Port Ice.Value to JavaScript mapping
Diffstat (limited to 'cpp/src/slice2js')
-rw-r--r-- | cpp/src/slice2js/Gen.cpp | 262 | ||||
-rw-r--r-- | cpp/src/slice2js/JsUtil.cpp | 138 | ||||
-rw-r--r-- | cpp/src/slice2js/JsUtil.h | 9 |
3 files changed, 144 insertions, 265 deletions
diff --git a/cpp/src/slice2js/Gen.cpp b/cpp/src/slice2js/Gen.cpp index e5e658f4aaa..2be5cd03f8a 100644 --- a/cpp/src/slice2js/Gen.cpp +++ b/cpp/src/slice2js/Gen.cpp @@ -643,6 +643,7 @@ Slice::Gen::RequireVisitor::writeRequires(const UnitPtr& p) if(_seenClass || _seenObjectSeq || _seenObjectDict) { requires["Ice"].push_back("Ice/Object"); + requires["Ice"].push_back("Ice/Value"); } if(_seenClass) { @@ -902,25 +903,20 @@ Slice::Gen::TypesVisitor::visitClassDefStart(const ClassDefPtr& p) const string localScope = getLocalScope(scope); const string name = fixId(p->name()); const string prxName = p->name() + "Prx"; - const string objectRef = "Ice.Object"; - const string prxRef = "Ice.ObjectPrx"; ClassList bases = p->bases(); ClassDefPtr base; string baseRef; - string basePrxRef; const bool hasBaseClass = !bases.empty() && !bases.front()->isInterface(); if(hasBaseClass) { base = bases.front(); bases.erase(bases.begin()); baseRef = getReference(scope, base->scoped()); - basePrxRef = getReference(scope, base->scoped() + "Prx"); } else { - baseRef = objectRef; - basePrxRef = prxRef; + baseRef = "Ice.Value"; } const DataMemberList allDataMembers = p->allDataMembers(); @@ -979,92 +975,123 @@ Slice::Gen::TypesVisitor::visitClassDefStart(const ClassDefPtr& p) _out << nl << "];"; } - _out << sp; - writeDocComment(p, getDeprecateReason(p, 0, "type")); - _out << nl << localScope << '.' << name << " = class"; - if(!p->isLocal() || hasBaseClass) - { - _out << " extends " << baseRef; - } - _out << sb; - if(!allParamNames.empty()) + if(!p->isInterface() || p->isLocal()) { - _out << nl << "constructor" << spar; - for(DataMemberList::const_iterator q = baseDataMembers.begin(); q != baseDataMembers.end(); ++q) + _out << sp; + writeDocComment(p, getDeprecateReason(p, 0, "type")); + _out << nl << localScope << '.' << name << " = class"; + if(!p->isLocal() || hasBaseClass) { - _out << fixId((*q)->name()); + _out << " extends " << baseRef; } - for(DataMemberList::const_iterator q = dataMembers.begin(); q != dataMembers.end(); ++q) + _out << sb; + if(!allParamNames.empty()) { - string value; - if((*q)->optional()) + _out << nl << "constructor" << spar; + for(DataMemberList::const_iterator q = baseDataMembers.begin(); q != baseDataMembers.end(); ++q) { - if((*q)->defaultValueType()) + _out << fixId((*q)->name()); + } + + for(DataMemberList::const_iterator q = dataMembers.begin(); q != dataMembers.end(); ++q) + { + string value; + if((*q)->optional()) { - value = writeConstantValue(scope, (*q)->type(), (*q)->defaultValueType(), (*q)->defaultValue()); + if((*q)->defaultValueType()) + { + value = writeConstantValue(scope, (*q)->type(), (*q)->defaultValueType(), (*q)->defaultValue()); + } + else + { + value = "undefined"; + } } else { - value = "undefined"; + if((*q)->defaultValueType()) + { + value = writeConstantValue(scope, (*q)->type(), (*q)->defaultValueType(), (*q)->defaultValue()); + } + else + { + value = getValue(scope, (*q)->type()); + } } + _out << (fixId((*q)->name()) + (value.empty() ? value : (" = " + value))); } - else + + _out << epar << sb; + if(!p->isLocal() || hasBaseClass) + { + _out << nl << "super" << spar << baseParamNames << epar << ';'; + } + writeInitDataMembers(dataMembers, scope); + _out << eb; + + if(!p->isLocal()) { - if((*q)->defaultValueType()) + if(p->compactId() != -1) { - value = writeConstantValue(scope, (*q)->type(), (*q)->defaultValueType(), (*q)->defaultValue()); + _out << sp; + _out << nl << "static get _iceCompactId()"; + _out << sb; + _out << nl << "return " << p->compactId() << ";"; + _out << eb; } - else + + if(!dataMembers.empty()) { - value = getValue(scope, (*q)->type()); + _out << sp; + _out << nl << "_iceWriteMemberImpl(ostr)"; + _out << sb; + writeMarshalDataMembers(dataMembers, optionalMembers); + _out << eb; + + _out << sp; + _out << nl << "_iceReadMemberImpl(istr)"; + _out << sb; + writeUnmarshalDataMembers(dataMembers, optionalMembers); + _out << eb; } } - _out << (fixId((*q)->name()) + (value.empty() ? value : (" = " + value))); } + _out << eb << ";"; - _out << epar << sb; - if(!p->isLocal() || hasBaseClass) + _out << sp; + if(!p->isLocal()) { - _out << nl << "super" << spar << baseParamNames << epar << ';'; - } - writeInitDataMembers(dataMembers, scope); - _out << eb; - } + bool preserved = p->hasMetaData("preserve-slice") && !p->inheritsMetaData("preserve-slice"); - _out << sp; - _out << nl << "static get _iceParent()"; - _out << sb; - if(!p->isLocal() || hasBaseClass) - { - _out << nl << "return " << baseRef << ";"; - } - else - { - _out << nl << "return undefined;"; + _out << nl << "Slice.defineValue(" << localScope << "." << name << ", " + << "iceC_" << getLocalScope(scoped, "_") << "_ids[" << scopedPos << "], " + << (preserved ? "true" : "false") ; + if(p->compactId() >= 0) + { + _out << ", " << p->compactId(); + } + _out << ");"; + } } - _out << eb; + // + // Define servant an proxy types for non local classes + // if(!p->isLocal()) { _out << sp; - _out << nl << "static get _iceIds()"; - _out << sb; - _out << nl << "return iceC_" << getLocalScope(scoped, "_") << "_ids;"; - _out << eb; - - _out << sp; - _out << nl << "static get _iceId()"; - _out << sb; - _out << nl << "return iceC_" << getLocalScope(scoped, "_") << "_ids[" << scopedPos << "];"; - _out << eb; - - _out << sp; - _out << nl << "_iceMostDerivedType()"; + writeDocComment(p, getDeprecateReason(p, 0, "type")); + _out << nl << localScope << "._" << p->name() << "Disp" << " = class extends "; + if(hasBaseClass) + { + _out << getLocalScope(base->scope()) << "._" << base->name() << "Disp"; + } + else + { + _out << "Ice.Object"; + } _out << sb; - _out << nl << "return " << localScope << "." << name << ";"; - _out << eb; - if(!bases.empty()) { @@ -1078,7 +1105,7 @@ Slice::Gen::TypesVisitor::visitClassDefStart(const ClassDefPtr& p) ClassDefPtr base = *q; if(base->isInterface()) { - _out << nl << getLocalScope(base->scope()) << "." << base->name(); + _out << nl << getLocalScope(base->scope()) << "._" << base->name()<< "Disp" ; if(++q != bases.end()) { _out << ", "; @@ -1093,36 +1120,8 @@ Slice::Gen::TypesVisitor::visitClassDefStart(const ClassDefPtr& p) _out << nl << "];"; _out << eb; } + _out << eb << ";"; - if(p->compactId() != -1) - { - _out << sp; - _out << nl << "static get _iceCompactId()"; - _out << sb; - _out << nl << "return " << p->compactId() << ";"; - _out << eb; - } - - if(!dataMembers.empty()) - { - _out << sp; - _out << nl << "_iceWriteMemberImpl(ostr)"; - _out << sb; - writeMarshalDataMembers(dataMembers, optionalMembers); - _out << eb; - - _out << sp; - _out << nl << "_iceReadMemberImpl(istr)"; - _out << sb; - writeUnmarshalDataMembers(dataMembers, optionalMembers); - _out << eb; - } - } - _out << eb << ";"; - - - if(!p->isLocal()) - { const string baseProxy = !p->isInterface() && base ? (getLocalScope(base->scope()) + "." + base->name() + "Prx") : "Ice.ObjectPrx"; @@ -1130,18 +1129,14 @@ Slice::Gen::TypesVisitor::visitClassDefStart(const ClassDefPtr& p) _out << nl << localScope << '.' << prxName << " = class extends " << baseProxy; _out << sb; - _out << sp; - _out << nl << "static ice_staticId()"; - _out << sb; - _out << nl << "return " << localScope << "." << name << "._iceId;"; - _out << eb; - _out << sp; - _out << nl << "static get _implements()"; - _out << sb; - _out << nl << "return ["; if(!bases.empty()) { + _out << sp; + _out << nl << "static get _implements()"; + _out << sb; + _out << nl << "return ["; + _out.inc(); for(ClassList::const_iterator q = bases.begin(); q != bases.end();) { @@ -1160,32 +1155,17 @@ Slice::Gen::TypesVisitor::visitClassDefStart(const ClassDefPtr& p) } } _out.dec(); + _out << "];"; + _out << eb; } - _out << "];"; - _out << eb; _out << eb << ";"; - if(p->hasMetaData("preserve-slice") && !p->inheritsMetaData("preserve-slice")) - { - _out << sp; - _out << nl << "Slice.PreservedObject(" << localScope << "." << name << ");"; - } - - // - // Register the compact id - // - if(p->compactId() >= 0) - { - // - // Also register the type using the stringified compact ID. - // - _out << nl << "Ice.CompactIdRegistry.set(" << p->compactId() << ", " << localScope << "." - << name << ".ice_staticId());"; - } - - _out << sp << nl << "Slice.defineOperations(" << localScope << '.' << name << ", " << localScope << '.' - << prxName; + _out << sp << nl << "Slice.defineOperations(" + << localScope << "._" << p->name() << "Disp, " + << localScope << '.' << prxName << ", " + << "iceC_" << getLocalScope(scoped, "_") << "_ids, " + << scopedPos; const OperationList ops = p->operations(); if(!ops.empty()) @@ -1773,17 +1753,18 @@ Slice::Gen::TypesVisitor::encodeTypeForOperation(const TypePtr& type) static const char* builtinTable[] = { - "0", // byte - "1", // bool - "2", // short - "3", // int - "4", // long - "5", // float - "6", // double - "7", // string - "8", // Ice.Object - "9", // Ice.ObjectPrx - "??" // LocalObject + "0", // byte + "1", // bool + "2", // short + "3", // int + "4", // long + "5", // float + "6", // double + "7", // string + "8", // Ice.Object + "9", // Ice.ObjectPrx + "??", // LocalObject + "10", // Ice.Value }; BuiltinPtr builtin = BuiltinPtr::dynamicCast(type); @@ -1825,7 +1806,14 @@ Slice::Gen::TypesVisitor::encodeTypeForOperation(const TypePtr& type) ClassDeclPtr cl = ClassDeclPtr::dynamicCast(type); if(cl) { - return "\"" + fixId(cl->scoped()) + "\""; + if(cl->isInterface()) + { + return "\"Ice.Value\""; + } + else + { + return "\"" + fixId(cl->scoped()) + "\""; + } } return "???"; diff --git a/cpp/src/slice2js/JsUtil.cpp b/cpp/src/slice2js/JsUtil.cpp index 641cc7c5632..8046c115102 100644 --- a/cpp/src/slice2js/JsUtil.cpp +++ b/cpp/src/slice2js/JsUtil.cpp @@ -28,16 +28,16 @@ using namespace IceUtil; using namespace IceUtilInternal; static string -lookupKwd(const string& name, bool mangleCasts = false) +lookupKwd(const string& name) { // // Keyword list. *Must* be kept in alphabetical order. // static const string keywordList[] = { - "break", "case", "catch", "class", "const", "continue", "debugger", "default", "delete", "do", "else", - "enum", "export", "extends", "false", "finally", "for", "function", "if", "implements", "import", "in", - "instanceof", "interface", "let", "new", "null", "package", "private", "protected", "public", "return", + "await", "break", "case", "catch", "class", "const", "continue", "debugger", "default", "delete", "do", + "else", "enum", "export", "extends", "false", "finally", "for", "function", "if", "implements", "import", + "in", "instanceof", "interface", "let", "new", "null", "package", "private", "protected", "public", "return", "static", "super", "switch", "this", "throw", "true", "try", "typeof", "var", "void", "while", "with", "yield" }; @@ -49,10 +49,7 @@ lookupKwd(const string& name, bool mangleCasts = false) { return "_" + name; } - if(mangleCasts && (name == "checkedCast" || name == "uncheckedCast")) - { - return "_" + name; - } + return name; } @@ -116,7 +113,7 @@ Slice::JsGenerator::isClassType(const TypePtr& type) // not scoped, but a JS keyword, return the "_"-prefixed name. // string -Slice::JsGenerator::fixId(const string& name, bool mangleCasts) +Slice::JsGenerator::fixId(const string& name) { if(name.empty()) { @@ -124,7 +121,7 @@ Slice::JsGenerator::fixId(const string& name, bool mangleCasts) } if(name[0] != ':') { - return lookupKwd(name, mangleCasts); + return lookupKwd(name); } const StringList ids = splitScopedName(name); @@ -143,119 +140,13 @@ Slice::JsGenerator::fixId(const string& name, bool mangleCasts) } string -Slice::JsGenerator::fixId(const ContainedPtr& cont, bool mangleCasts) -{ - return fixId(cont->name(), mangleCasts); -} - -string -Slice::JsGenerator::getOptionalFormat(const TypePtr& type) -{ - BuiltinPtr bp = BuiltinPtr::dynamicCast(type); - if(bp) - { - switch(bp->kind()) - { - case Builtin::KindByte: - case Builtin::KindBool: - { - return "Ice.OptionalFormat.F1"; - } - case Builtin::KindShort: - { - return "Ice.OptionalFormat.F2"; - } - case Builtin::KindInt: - case Builtin::KindFloat: - { - return "Ice.OptionalFormat.F4"; - } - case Builtin::KindLong: - case Builtin::KindDouble: - { - return "Ice.OptionalFormat.F8"; - } - case Builtin::KindString: - { - return "Ice.OptionalFormat.VSize"; - } - case Builtin::KindObject: - case Builtin::KindValue: - { - return "Ice.OptionalFormat.Class"; - } - case Builtin::KindObjectProxy: - { - return "Ice.OptionalFormat.FSize"; - } - case Builtin::KindLocalObject: - { - assert(false); - break; - } - } - } - - if(EnumPtr::dynamicCast(type)) - { - return "Ice.OptionalFormat.Size"; - } - - SequencePtr seq = SequencePtr::dynamicCast(type); - if(seq) - { - return seq->type()->isVariableLength() ? "Ice.OptionalFormat.FSize" : "Ice.OptionalFormat.VSize"; - } - - DictionaryPtr d = DictionaryPtr::dynamicCast(type); - if(d) - { - return (d->keyType()->isVariableLength() || d->valueType()->isVariableLength()) ? - "Ice.OptionalFormat.FSize" : "Ice.OptionalFormat.VSize"; - } - - StructPtr st = StructPtr::dynamicCast(type); - if(st) - { - return st->isVariableLength() ? "Ice.OptionalFormat.FSize" : "Ice.OptionalFormat.VSize"; - } - - if(ProxyPtr::dynamicCast(type)) - { - return "Ice.OptionalFormat.FSize"; - } - - ClassDeclPtr cl = ClassDeclPtr::dynamicCast(type); - assert(cl); - return "Ice.OptionalFormat.Class"; -} - -string -Slice::JsGenerator::getStaticId(const TypePtr& type) +Slice::JsGenerator::fixId(const ContainedPtr& cont) { - BuiltinPtr b = BuiltinPtr::dynamicCast(type); - ClassDeclPtr cl = ClassDeclPtr::dynamicCast(type); - - assert((b && b->kind() == Builtin::KindObject) || cl); - - if(b) - { - return "Ice.ObjectImpl.ice_staticId()"; - } - else if(cl->isInterface()) - { - ContainedPtr cont = ContainedPtr::dynamicCast(cl->container()); - assert(cont); - return fixId(cont->scoped()) + "." + cl->name() + "Disp_.ice_staticId()"; - } - else - { - return fixId(cl->scoped()) + ".ice_staticId()"; - } + return fixId(cont->name()); } string -Slice::JsGenerator::typeToString(const TypePtr& type, bool optional) +Slice::JsGenerator::typeToString(const TypePtr& type) { if(!type) { @@ -272,9 +163,10 @@ Slice::JsGenerator::typeToString(const TypePtr& type, bool optional) "Number", // float "Number", // double "String", - "Ice.Object", + "Ice.Value", "Ice.ObjectPrx", - "Object" + "Object", + "Ice.Value" }; BuiltinPtr builtin = BuiltinPtr::dynamicCast(type); @@ -298,7 +190,9 @@ Slice::JsGenerator::typeToString(const TypePtr& type, bool optional) DictionaryPtr d = DictionaryPtr::dynamicCast(type); if(d) { - return "Ice.HashMap"; + const TypePtr keyType = d->keyType(); + BuiltinPtr b = BuiltinPtr::dynamicCast(keyType); + return ((b && b->kind() == Builtin::KindLong) || StructPtr::dynamicCast(keyType)) ? "Ice.HashMap" : "Map"; } ContainedPtr contained = ContainedPtr::dynamicCast(type); diff --git a/cpp/src/slice2js/JsUtil.h b/cpp/src/slice2js/JsUtil.h index b379a4c7977..f6874ba665b 100644 --- a/cpp/src/slice2js/JsUtil.h +++ b/cpp/src/slice2js/JsUtil.h @@ -25,12 +25,9 @@ public: protected: static bool isClassType(const TypePtr&); - static std::string localProxyHelper(const TypePtr&); - static std::string fixId(const std::string&, bool = false); - static std::string fixId(const ContainedPtr&, bool = false); - static std::string getOptionalFormat(const TypePtr&); - static std::string getStaticId(const TypePtr&); - static std::string typeToString(const TypePtr&, bool = false); + static std::string fixId(const std::string&); + static std::string fixId(const ContainedPtr&); + static std::string typeToString(const TypePtr&); static std::string getLocalScope(const std::string&, const std::string& separator = "."); static std::string getReference(const std::string&, const std::string&); |