Skip to content

Instantly share code, notes, and snippets.

@pmuellr
Created September 29, 2011 17:22
Show Gist options
  • Select an option

  • Save pmuellr/1251327 to your computer and use it in GitHub Desktop.

Select an option

Save pmuellr/1251327 to your computer and use it in GitHub Desktop.
IDL for JavaScript in CoffeeScript
Classes = []
Callbacks = []
#===============================================================
class Class
constructor: (@name, properties) ->
@properties = []
@methods = []
@staticProperties = []
@staticMethods = []
for name, property of properties
property.name = name
if property.isMethod
if property.isStatic
@staticMethods.push property
else
@methods.push property
else
if property.isStatic
@staticProperties.push property
else
@properties.push property
class FunctionDesc
constructor: (@name, properties) ->
@returns = "void"
@parameters = []
for name, type of properties
if name is "returns"
@result = type
else
@parameters.push [name, type]
definitionString: ->
parms = for parameter in @parameters
"#{parameter[0]}: #{parameter[1]}"
parms = parms.join(", ")
return "#{@name}(#{parms}): #{@returns}"
class Method extends FunctionDesc
constructor: (@name, properties) ->
super
@isStatic = false
isMethod: true
isProperty: false
class Property
constructor: (@name, @type) ->
@isStatic = false
isMethod: false
isProperty: true
#===============================================================
aCallback = (name, properties) ->
callback = new FunctionDesc(name, properties)
Callbacks.push callback
aClass = (name, properties) ->
cls = new Class(name, properties)
Classes.push cls
aMethod = (properties) ->
new Method(null, properties)
aStaticMethod = (properties) ->
result = new Method(null, properties)
result.isStatic = true
result
aProperty = (type) ->
new Property(null, type)
aStaticProperty = (type) ->
result = new Property(null, type)
result.isStatic = true
result
printResults = ->
console.log("")
console.log("Callbacks:")
for callback in Callbacks
console.log ""
console.log " #{callback.name}"
console.log " #{callback.definitionString()}"
console.log("")
console.log("Classes:")
for cls in Classes
console.log ""
console.log " #{cls.name}"
for property in cls.staticProperties
console.log " static #{property.name}: #{property.type}"
for method in cls.staticMethods
console.log " static #{method.definitionString()}"
for property in cls.properties
console.log " #{property.name}: #{property.type}"
for method in cls.methods
console.log " #{method.definitionString()}"
setTimeout printResults, 100
#===============================================================
# Below is the IDL that we'd read from a file, prepend with
# the definitions above, and run.
#===============================================================
aClass "BarcodeScannerResult"
text: aProperty "string"
type: aProperty "string"
cancelled: aProperty "boolean"
aCallback "BarcodeScannerSuccessCB"
result: "BarcodeScannerResult"
aCallback "BarcodeScannerFailureCB"
message: "string"
aClass "BarcodeScanner"
scan: aMethod
returns: "void"
success: "BarcodeScannerSuccessCB"
failure: "BarcodeScannerFailureCB"
scanSync: aMethod
returns: "BarcodeScannerResult"
aClass "SomeClass"
Constant1: aStaticProperty "number"
Constant2: aStaticProperty "number"
create: aStaticMethod
returns: "SoemClass"
parm1: "string"
parm2: "number"
id: aProperty "string"
toStringWithArgs: aMethod
returns: "string"
parm1: "number"
parm2: "string"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment