Max 5 API Reference
00001 /** 00002 @file 00003 simplejs - a max object used as a JavaScript class 00004 00005 @ingroup examples 00006 */ 00007 00008 #include "ext.h" // standard Max include, always required 00009 #include "ext_obex.h" // required for new style Max object 00010 00011 ////////////////////////// object struct 00012 typedef struct _simplejs 00013 { 00014 t_object ob; // the object itself (must be first) 00015 double myattr; 00016 } t_simplejs; 00017 00018 ///////////////////////// function prototypes 00019 //// standard set 00020 void *simplejs_new(t_symbol *s, long argc, t_atom *argv); 00021 void simplejs_free(t_simplejs *x); 00022 void simplejs_print(t_simplejs *x, t_symbol *s, long ac, t_atom *av); 00023 t_max_err simplejs_doabs(t_simplejs *x, t_symbol *s, long ac, t_atom *av, t_atom *rv); 00024 00025 00026 //////////////////////// global class pointer variable 00027 void *simplejs_class; 00028 00029 00030 int main(void) 00031 { 00032 t_class *c; 00033 00034 c = class_new("simplejs", (method)simplejs_new, (method)simplejs_free, (long)sizeof(t_simplejs), 00035 0L /* leave NULL!! */, A_GIMME, 0); 00036 00037 class_addmethod(c, (method)simplejs_print, "print", 0); 00038 class_addmethod(c, (method)simplejs_doabs, "doAbs", A_GIMMEBACK, 0); 00039 00040 CLASS_ATTR_DOUBLE(c, "myattr", 0, t_simplejs, myattr); 00041 00042 c->c_flags = CLASS_FLAG_POLYGLOT; 00043 class_register(CLASS_NOBOX, c); 00044 simplejs_class = c; 00045 00046 return 0; 00047 } 00048 00049 void simplejs_free(t_simplejs *x) 00050 { 00051 ; 00052 } 00053 00054 void *simplejs_new(t_symbol *s, long argc, t_atom *argv) 00055 { 00056 t_simplejs *x = NULL; 00057 00058 // object instantiation, NEW STYLE 00059 if (x = (t_simplejs *)object_alloc(simplejs_class)) { 00060 // Initialize values 00061 x->myattr = 74.; 00062 } 00063 return (x); 00064 } 00065 00066 void simplejs_print(t_simplejs *x, t_symbol *s, long ac, t_atom *av) 00067 { 00068 post("The value of myattr is: %f", x->myattr); 00069 } 00070 00071 t_max_err simplejs_doabs(t_simplejs *x, t_symbol *s, long ac, t_atom *av, t_atom *rv) 00072 { 00073 t_atom a[1]; 00074 double f = 0; 00075 00076 if (ac) { 00077 if (atom_gettype(av) == A_LONG) 00078 f = (double)abs(atom_getlong(av)); 00079 else if( atom_gettype(av) == A_FLOAT) 00080 f = fabs(atom_getfloat(av)); 00081 } else 00082 error("missing argument for method doAbs()"); 00083 00084 // store the result in the a array. 00085 atom_setfloat(a, f); 00086 00087 // return the result to js 00088 atom_setobj(rv, object_new(gensym("nobox"), gensym("atomarray"), 1, a)); 00089 00090 return MAX_ERR_NONE; 00091 }
Copyright © 2008, Cycling '74