Commits

Fredrik Johansson committed 2469b14

Trying to fix Enums in lists for Javascript

  • Participants
  • Parent commits c9df010

Comments (0)

Files changed (2)

+
+syntax: glob
+target
+logs
+.classpath
+.project
+.settings
+bin
+
+server/firebase/games/testgame/client/src/main/webapp/js-ext/testgame-protocol.js
+server/firebase/games/testgame/client/src/main/webapp/js-ext/firebase-protocol.js
+server/firebase/games/testgame/client/src/main/webapp/js-ext/firebase-js-api.js
+*.iml
+.idea
+

File maven/protocol-generator-plugin/src/main/scripts/javascript_generator.rb

   'uint64' => 'uint',
   'string' => 'String',
   'bool'   => 'Boolean'
-}
-
-JAVASCRIPT_TYPES2 = {
-  'uint8'  => 'UnsignedByte',
-  'int8'   => 'Byte',
-  'uint16' => 'UnsignedShort',
-  'int16'  => 'Short',
-  'int32'  => 'Int',
-  'uint32' => 'UnsignedInt',
-  'int64'  => 'Long',
-  'uint64' => 'Long',
-  'string' => 'String',
-  'bool'   => 'Boolean'
+}
+
+JAVASCRIPT_TYPES2 = {
+  'uint8'  => 'UnsignedByte',
+  'int8'   => 'Byte',
+  'uint16' => 'UnsignedShort',
+  'int16'  => 'Short',
+  'int32'  => 'Int',
+  'uint32' => 'UnsignedInt',
+  'int64'  => 'Long',
+  'uint64' => 'Long',
+  'string' => 'String',
+  'bool'   => 'Boolean'
 }
 
 
 def jstype(generic_type)
   JAVASCRIPT_TYPES[generic_type] || camelcasetype(generic_type)
 end
-
-def jstype2(generic_type)
-  JAVASCRIPT_TYPES2[generic_type] || camelcasetype(generic_type)
-end
+
+def jstype2(generic_type)
+  JAVASCRIPT_TYPES2[generic_type] || camelcasetype(generic_type)
+end
 
 def fbuiltin?(generic_type)
   JAVASCRIPT_TYPES.has_key?(generic_type)
   str.split('_').map{|e| e.capitalize}.join
 end
 
-class JavaScriptOut
-  
+class JavaScriptOut
+  
   
   def initialize(output_file, empty)
     @indent_level = 0
-    @out = output_file
+    @out = output_file
     @index = 0 
     @empty = empty
   end
     [:start_class, :nested_types, :members, :default_constructor, :save, :load, :sysout, :end_class]
   end
 
-  def start_class(name, class_id)
+  def start_class(name, class_id)
     @classname = $javascript_namespace + "." + camelcasetype(name)
 
     @classid = "#{$javascript_namespace}.#{camelcasetype(name)}.CLASSID = #{class_id};\n"    
     
     wi("#{$javascript_namespace}.#{camelcasetype(name)} = function(){")
     @indent_level += 1
-    if !class_id.to_s.empty?
+    if !class_id.to_s.empty?
       wi("this.classId = function() {")
       wi("return #{$javascript_namespace}.#{camelcasetype(name)}.CLASSID;",1)
       wi("};")
   end  
     
   def end_scope
-    @indent_level -= 1
+    @indent_level -= 1
     @index = 0
     wi("};")
   end
-
+
 def end_save
-     if !@empty
+     if !@empty
       wi("return byteArray;")
      else
        wi("return [];")
-     end
-     @indent_level -= 1
-     wi("};")
-  end
+     end
+     @indent_level -= 1
+     wi("};")
+  end
 
   def end_class
     @indent_level -= 1
     wi("this.#{camelcase(list.name)} = [];")
   end
 
-  def member_enum(enum)
-    enumtype = enum.type
-    enumtype = enumtype.capitalize
+  def member_enum(enum)
+    enumtype = enum.type
+    enumtype = enumtype.capitalize
     enumtype= camelcase(enumtype)
     name = camelcase(enum.name)
     # wi("this.#{name} = {};")
     wi("this.save = function() {")
     @indent_level += 1
     @index = 0
-    if !@empty
+    if !@empty
         wi("var byteArray = new FIREBASE.ByteArray();")
-    end
+    end
   end
 
   def save_scalar(scalar)
 
   def save_list(list)
     name = camelcase(list.name)
-     ltype = jstype2(list.obj_type)
-  
-    if(list.obj_type == "uint8" || list.obj_type == "int8")
-      wi("byteArray.writeInt(this.#{name}.length);")
-      wi("byteArray.writeArray(this.#{name});")
-    else
+     ltype = jstype2(list.obj_type)
+  
+    if(list.obj_type == "uint8" || list.obj_type == "int8")
+      wi("byteArray.writeInt(this.#{name}.length);")
+      wi("byteArray.writeArray(this.#{name});")
+    else
           # Save as List of Objects
-      wi("byteArray.writeInt(this.#{name}.length);")
-      if(@index == 0)
-        wi("var i;")
-        @index += 1
-      end
+      wi("byteArray.writeInt(this.#{name}.length);")
+      if(@index == 0)
+        wi("var i;")
+        @index += 1
+      end
       
-      wi("for( i = 0; i < this.#{name}.length; i ++) {")
-      #@indent_level += 1
+      wi("for( i = 0; i < this.#{name}.length; i ++) {")
+      #@indent_level += 1
       if(fbuiltin?(list.obj_type))
         wi("byteArray.write#{ltype}(this.#{name}[i]);",1)
       else
         wi("byteArray.writeArray(this.#{name}[i].save());",1) 
-      end
+      end
       wi("}");
     end
   end
     wi("this.load = function(byteArray) {")
     @indent_level += 1
     @index = 0
-#    if !@empty
+#    if !@empty
 #      wi("var byteArray = new FIREBASE.ByteArray(buffer);")
 #    end
   end
 
-  def load_enum(enum)
+  def load_enum(enum)
     name = enum.type.capitalize
     name = camelcase(name)
     wi("this.#{camelcase(enum.name)} =  #{$javascript_namespace}.#{name}Enum.make#{name}Enum(byteArray.readUnsignedByte());")
       wi("var #{name}Count = byteArray.readInt();")
       wi("this.#{name} = byteArray.readArray(#{name}Count);")
     
-    elsif(fbuiltin?(list.obj_type))
-      if ( @index == 0 )
-        wi("var i;")
-        @index += 1
-      end
+    elsif(fbuiltin?(list.obj_type))
+      if ( @index == 0 )
+        wi("var i;")
+        @index += 1
+      end
         
       wi("var #{name}Count = byteArray.readInt();")
-      wi("this.#{name} = [];")
-      wi("for (i = 0; i < #{name}Count; i ++) {")
-      wi("this.#{name}.push(byteArray.read#{ltype}());",1)
-      wi("}");
+      wi("this.#{name} = [];")
+      wi("for (i = 0; i < #{name}Count; i ++) {")
+      wi("this.#{name}.push(byteArray.read#{ltype}());",1)
+      wi("}");
       
     else
-     if ( @index == 0 )
-        wi("var i;")
-        @index += 1
-      end
+     if ( @index == 0 )
+        wi("var i;")
+        @index += 1
+      end
       
       wi("var #{name}Count = byteArray.readInt();")
       
       wi("for( i = 0; i < #{name}Count; i ++) {")
       if(fbuiltin?(list.obj_type))
         wi("this.#{name}.push(byteArray.read#{ltype}());",1)
+      elsif(enum?(list.obj_type))
+      	wi("o#{ltype} =  #{$javascript_namespace}.#{ltype}Enum.make#{ltype}Enum(byteArray.readUnsignedByte());", 1)
+      	wi("this.#{name}.push(o#{ltype});",1)
       else
         wi("o#{ltype} = new #{$javascript_namespace}.#{ltype}();",1)
         wi("o#{ltype}.load(byteArray);",1)
         wi("this.#{name}.push(o#{ltype});",1)
       end
-      wi("}")
+      wi("}")
       @index += 1
     end
   end
     @out.write(line + "\n")
   end
 
-  def JavaScriptOut.gen_enum_deserializer(name, values)
-    name = name.capitalize
+  def JavaScriptOut.gen_enum_deserializer(name, values)
+    name = name.capitalize
     name = camelcase(name)
     str = "\n#{$javascript_namespace}.#{name}Enum.make#{name}Enum = function(value) {\n"
     str<< "    switch(value) {\n"
     values.each_with_index {|v,i| str << "        case #{i}: return #{$javascript_namespace}.#{name}Enum.#{v};\n" }
-    str<< "    }\n"
+    str<< "    }\n"
     str<< "    return -1;\n"
     str<< "};\n"
     str
  end
 
   def JavaScriptOut.gen_enum(f, name, values)
-    name = name.capitalize
-    name = camelcase(name)
-    values.each_with_index {|v,i| f.write("    #{$javascript_namespace}.#{name}Enum.#{v} = #{i};\n")}
+    name = name.capitalize
+    name = camelcase(name)
+    values.each_with_index {|v,i| f.write("    #{$javascript_namespace}.#{name}Enum.#{v} = #{i};\n")}
   end
 
   def JavaScriptOut.generate_enums(filename, enums, package)
-    enums.each_pair do |enum, values|
-      name = enum.capitalize
-      name = camelcase(name)
-      
-      enum_file_name = "#{filename}#{name}Enum.js"
-      File.open(enum_file_name, 'w') do |enum_file|
+    enums.each_pair do |enum, values|
+      name = enum.capitalize
+      name = camelcase(name)
+      
+      enum_file_name = "#{filename}#{name}Enum.js"
+      File.open(enum_file_name, 'w') do |enum_file|
       enum_file.write(JavaScriptOut.file_header(package))
-      enum_file.write("#{$javascript_namespace}.#{name}Enum = function() {\n")
+      enum_file.write("#{$javascript_namespace}.#{name}Enum = function() {\n")
       enum_file.write("};\n")
       gen_enum(enum_file, enum.capitalize, values)
       enum_file.write(gen_enum_deserializer(enum.capitalize, values))
       enum_file.write(gen_enum_toString(enum.capitalize, values))
-      enum_file.write(JavaScriptOut.file_footer(package))
-      puts "Wrote #{enum_file_name}" unless $quiet
+      enum_file.write(JavaScriptOut.file_footer(package))
+      puts "Wrote #{enum_file_name}" unless $quiet
     end
     end
   end
   
   def JavaScriptOut.file_header(package)
   end
-
-  def sysout(list)
-    wi("\n")
+
+  def sysout(list)
+    wi("\n")
     wi("this.getNormalizedObject = function() {")
     wi("var normalizedObject = {};",1)
     wi("var i;",1)
       end
     end
     wi("return normalizedObject;",1)
-    wi("};\n")
-  end
-
+    wi("};\n")
+  end
+
 def JavaScriptOut.file_footer(package)
 
-   <<FOOTER_END
+   <<FOOTER_END
 
-// I AM AUTO-GENERATED, DON'T CHECK ME INTO SUBVERSION (or else...)
-
-FOOTER_END
-end
-
+// I AM AUTO-GENERATED, DON'T CHECK ME INTO SUBVERSION (or else...)
+
+FOOTER_END
 end
+
+end