Skip to content

Commit 195a371

Browse files
author
jianglong0156
committed
issue cocos2d#1108: add cleanScript and testcase, the testcase will be improved on next commit
1 parent f37d3a3 commit 195a371

File tree

6 files changed

+189
-1
lines changed

6 files changed

+189
-1
lines changed

frameworks/js-bindings/bindings/manual/ScriptingCore.cpp

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,24 @@ bool JSBCore_os(JSContext *cx, uint32_t argc, jsval *vp)
354354
return true;
355355
};
356356

357+
bool JSB_cleanScript(JSContext *cx, uint32_t argc, jsval *vp)
358+
{
359+
if (argc != 1)
360+
{
361+
JS_ReportError(cx, "Invalid number of arguments in JSB_cleanScript");
362+
return false;
363+
}
364+
jsval *argv = JS_ARGV(cx, vp);
365+
JSString *jsPath = JSVAL_TO_STRING(argv[0]);
366+
JSB_PRECONDITION2(jsPath, cx, false, "Error js file in clean script");
367+
JSStringWrapper wrapper(jsPath);
368+
ScriptingCore::getInstance()->cleanScript(wrapper.get());
369+
370+
JS_SET_RVAL(cx, vp, JSVAL_VOID);
371+
372+
return true;
373+
};
374+
357375
bool JSB_core_restartVM(JSContext *cx, uint32_t argc, jsval *vp)
358376
{
359377
JSB_PRECONDITION2(argc==0, cx, false, "Invalid number of arguments in executeScript");
@@ -394,11 +412,12 @@ void registerDefaultClasses(JSContext* cx, JSObject* global) {
394412
JS_DefineFunction(cx, global, "log", ScriptingCore::log, 0, JSPROP_READONLY | JSPROP_PERMANENT);
395413
JS_DefineFunction(cx, global, "executeScript", ScriptingCore::executeScript, 1, JSPROP_READONLY | JSPROP_PERMANENT);
396414
JS_DefineFunction(cx, global, "forceGC", ScriptingCore::forceGC, 0, JSPROP_READONLY | JSPROP_PERMANENT);
397-
415+
398416
JS_DefineFunction(cx, global, "__getPlatform", JSBCore_platform, 0, JSPROP_READONLY | JSPROP_PERMANENT);
399417
JS_DefineFunction(cx, global, "__getOS", JSBCore_os, 0, JSPROP_READONLY | JSPROP_PERMANENT);
400418
JS_DefineFunction(cx, global, "__getVersion", JSBCore_version, 0, JSPROP_READONLY | JSPROP_PERMANENT);
401419
JS_DefineFunction(cx, global, "__restartVM", JSB_core_restartVM, 0, JSPROP_READONLY | JSPROP_PERMANENT | JSPROP_ENUMERATE );
420+
JS_DefineFunction(cx, global, "__cleanScript", JSB_cleanScript, 1, JSPROP_READONLY | JSPROP_PERMANENT);
402421
}
403422

404423
static void sc_finalize(JSFreeOp *freeOp, JSObject *obj) {

frameworks/js-bindings/bindings/script/jsb_boot.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1332,6 +1332,12 @@ cc._initSys = function(config, CONFIG_KEY){
13321332
__restartVM();
13331333
};
13341334

1335+
// clean a singal js file
1336+
locSys.cleanScript = function(jsFile) {
1337+
__cleanScript(jsFile);
1338+
};
1339+
1340+
13351341
locSys.dump = function(){
13361342
var self = this;
13371343
var str = "";

samples/js-tests/project.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@
8383

8484
"src/ExtensionsTest/ExtensionsTest.js",
8585
"src/ExtensionsTest/AssetsManagerTest/AssetsManagerTest.js",
86+
"src/ExtensionsTest/ScriptTest/ScriptTest.js",
87+
"src/ExtensionsTest/ScriptTest/ScriptTestTempFile.js",
8688
"src/ExtensionsTest/ControlExtensionTest/CCControlSceneManager.js",
8789
"src/ExtensionsTest/ControlExtensionTest/CCControlScene.js",
8890
"src/ExtensionsTest/ControlExtensionTest/CCControlButtonTest/CCControlButtonTest.js",

samples/js-tests/src/ExtensionsTest/ExtensionsTest.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,18 @@ if (cc.sys.isNative && cc.sys.OS_WINDOWS != cc.sys.os) {
125125
});
126126
}
127127

128+
//if (cc.sys.isNative && cc.sys.OS_WINDOWS != cc.sys.os) {
129+
extensionsTestItemNames.push({
130+
itemTitle:"ScriptTestLayer",
131+
testScene:function () {
132+
var testScene = new ScriptTestLoaderScene();
133+
if (testScene) {
134+
testScene.runThisTest();
135+
}
136+
}
137+
});
138+
//}
139+
128140
var ExtensionsMainLayer = cc.Layer.extend({
129141
onEnter:function () {
130142
this._super();
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/****************************************************************************
2+
Copyright (c) 2008-2010 Ricardo Quesada
3+
Copyright (c) 2011-2012 cocos2d-x.org
4+
Copyright (c) 2013-2014 Chukong Technologies Inc.
5+
6+
http://www.cocos2d-x.org
7+
8+
Permission is hereby granted, free of charge, to any person obtaining a copy
9+
of this software and associated documentation files (the "Software"), to deal
10+
in the Software without restriction, including without limitation the rights
11+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
copies of the Software, and to permit persons to whom the Software is
13+
furnished to do so, subject to the following conditions:
14+
15+
The above copyright notice and this permission notice shall be included in
16+
all copies or substantial portions of the Software.
17+
18+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
THE SOFTWARE.
25+
****************************************************************************/
26+
27+
var currentScene = 0;
28+
29+
var ScriptTestLayer = BaseTestLayer.extend({
30+
_tempLayer:null,
31+
clickMeShowTempLayer:function () {
32+
if (this._tempLayer != null)
33+
{
34+
this._tempLayer.removeFromParent();
35+
}
36+
this._tempLayer = new ScriptTestTempLayer();
37+
this.addChild(this._tempLayer);
38+
},
39+
clickMeReloadTempLayer:function(){
40+
cc.sys.cleanScript("ScriptTestTempFile.js");
41+
// insert from the sdcard
42+
// to do
43+
if (cc.sys.os == cc.sys.OS_ANDROID)
44+
{
45+
jsb.fileUtils.addSearchPath("/mnt/sdcard/newScript", true);
46+
require("ScriptTestTempFile.js");
47+
}
48+
49+
this.clickMeShowTempLayer();
50+
},
51+
ctor : function () {
52+
this._super();
53+
54+
var menu = new cc.Menu();
55+
menu.setPosition(cc.p(0, 0));
56+
menu.width = winSize.width;
57+
menu.height = winSize.height;
58+
this.addChild(menu, 1);
59+
var item1 = new cc.MenuItemLabel(new cc.LabelTTF("Click me show tempLayer", "Arial", 22), this.clickMeShowTempLayer, this);
60+
menu.addChild(item1);
61+
62+
var item2 = new cc.MenuItemLabel(new cc.LabelTTF("Click me reload tempLayer", "Arial", 22), this.clickMeReloadTempLayer, this);
63+
menu.addChild(item2);
64+
65+
menu.alignItemsVerticallyWithPadding(8);
66+
menu.setPosition(cc.pAdd(cc.visibleRect.left, cc.p(+180, 0)));
67+
},
68+
69+
getTitle : function() {
70+
return "ScriptTest";
71+
},
72+
onNextCallback : function () {
73+
if (currentScene < 2)
74+
{
75+
currentScene++;
76+
}
77+
else currentScene = 0;
78+
var scene = new ScriptTestLoaderScene();
79+
scene.runThisTest();
80+
},
81+
82+
onBackCallback : function () {
83+
if (currentScene > 0)
84+
{
85+
currentScene--;
86+
}
87+
else currentScene = 2;
88+
var scene = new ScriptTestLoaderScene();
89+
scene.runThisTest();
90+
}
91+
});
92+
93+
var RestartGameLayer = BaseTestLayer.extend({
94+
ctor : function () {
95+
this._super();
96+
}
97+
});
98+
var ScriptTestScene = TestScene.extend({
99+
ctor : function () {
100+
this._super();
101+
var layer = new ScriptTestLayer();
102+
this.addChild(layer);
103+
}
104+
});
105+
106+
var ScriptTestLoaderScene = TestScene.extend({
107+
runThisTest : function () {
108+
var scene = new ScriptTestScene();
109+
cc.director.runScene(scene);
110+
}
111+
});
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/****************************************************************************
2+
Copyright (c) 2008-2010 Ricardo Quesada
3+
Copyright (c) 2011-2012 cocos2d-x.org
4+
Copyright (c) 2013-2014 Chukong Technologies Inc.
5+
6+
http://www.cocos2d-x.org
7+
8+
Permission is hereby granted, free of charge, to any person obtaining a copy
9+
of this software and associated documentation files (the "Software"), to deal
10+
in the Software without restriction, including without limitation the rights
11+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
copies of the Software, and to permit persons to whom the Software is
13+
furnished to do so, subject to the following conditions:
14+
15+
The above copyright notice and this permission notice shall be included in
16+
all copies or substantial portions of the Software.
17+
18+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
THE SOFTWARE.
25+
****************************************************************************/
26+
27+
var ScriptTestTempLayer = cc.Layer.extend({
28+
ctor : function () {
29+
this._super();
30+
31+
var labelTest = new cc.LabelTTF("this is the ScriptTestTempLayer old file", "Verdana", 32, cc.size(winSize.width, 50), cc.TEXT_ALIGNMENT_CENTER);
32+
var size = cc.winSize;
33+
labelTest.setPosition(size.width / 2, size.height / 4);
34+
this.addChild(labelTest);
35+
36+
}
37+
38+
});

0 commit comments

Comments
 (0)