1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 | #include "js.h" enum tagMY_PEOPLE {MY_NAME,MY_ADDRESS}; static JSBool GetPeopleProperty (JSContext *cx, JSObject *obj, jsval id, jsval *vp); static JSBool SetPeopleProperty (JSContext *cx, JSObject *obj, jsval id, jsval *vp); static JSBool PeoplePrint(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval); typedef struct{ char name[16]; char addr[64];}PeopleInfo; static PeopleInfo m_ainfo={"myName","myAddress"}; /*定义属性的 GETTER*/ static JSBool GetPeopleProperty (JSContext *cx, JSObject *obj, jsval id, jsval *vp) { if (JSVAL_IS_INT(id)) { switch (JSVAL_TO_INT(id)) { case MY_NAME: *vp=STRING_TO_JSVAL (JS_NewStringCopyZ (cx,m_ainfo.name)); break; case MY_ADDRESS: *vp=STRING_TO_JSVAL (JS_NewStringCopyZ (cx,m_ainfo.addr)); break; } } return JS_TRUE; } /*定义属性的SETTER*/ static JSBool SetPeopleProperty (JSContext *cx, JSObject *obj, jsval id, jsval *vp) { if (JSVAL_IS_INT(id)) { switch (JSVAL_TO_INT(id)) { case MY_NAME: strncpy (m_ainfo.name, JS_GetStringBytes (jss), 15); break; case MY_ADDRESS: strncpy (m_ainfo.addr, JS_GetStringBytes (jss), 63); break; } } return JS_TRUE; } /*定义print方法*/ static JSBool PeoplePrint(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) { fprintf(stdout,"My Name is %s.\nMy Addr is %s.\n",m_ainfo.name,m_ainfo.addr); return JS_TRUE; } void main() { JSString* jss; char buf[5120]; int len; jsval rval; JSRuntime *rt; JSContext *cx; JSObject *globalObj,*PeopleObj; JSClass global_class = { "global",0, JS_PropertyStub, JS_PropertyStub,JS_PropertyStub, JS_PropertyStub, JS_EnumerateStub, JS_ResolveStub,JS_ConvertStub, JS_FinalizeStub }; /*定义People类的属性数组*/ static JSPropertySpec PeopleProperties[] = { {"name", MY_NAME, JSPROP_ENUMERATE }, {"address", MY_ADDRESS, JSPROP_ENUMERATE }, {0} } ; /*定义People类的方法数组*/ static JSFunctionSpec PeopleMethods[] = { {"print", PeoplePrint, 0}, {0} }; /*定义People类*/ static JSClass PeopleClass = { "people",0, JS_PropertyStub,JS_PropertyStub, GetPeopleProperty, SetPeopleProperty, JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, JS_FinalizeStub }; typedef struct{} /* 初始化JS RunTime,返回结果给rt */ rt = JS_Init(1000000L); if (!rt)return; /* 创建一个上下文,并将其与JS RunTime关联起来 */ cx = JS_NewContext(rt, 5120); if (!cx)return; /* 创建全局对象 */ if (!(globalObj = JS_NewObject (cx, &global_class, NULL, NULL)))return; /* 实例化内置对象和全局对象*/ JS_InitStandardClasses (cx, globalObj); /*实例化People对象*/ PeopleObj = JS_DefineObject (cx, globalObj, "People", &PeopleClass, 0,JSPROP_ENUMERATE); /*创建对象的属性*/ JS_DefineProperties (cx,PeopleObj, PeopleProperties); /*创建对象的方法*/ JS_DefineFunctions (cx,PeopleObj, PeopleMethods); FILE* fp; /*打开文件,读入脚本*/ if (!(fp = fopen ("test.js", "r")))return; len = fread (buf, 1, 5120, fp); fclose (fp); if (len <= 0)return; /*执行一段脚本*/ JS_EvaluateScript (cx, globalObj, buf, len, "", 1, &rval); jss = JS_ValueToString (cx, rval); fprintf(stdout,"The result is: %s",JS_GetStringBytes (jss)); /*释放上下文*/ JS_DestroyContext(cx); /*释放RunTime*/ JS_DestroyRuntime(rt); return; } |
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/) | Powered by Discuz! 7.0.0 |