Skip to content

Commit 1a5eb21

Browse files
authored
Merge pull request #201 from krunt/origin/_v8
fixes to http module to support polaroid example
2 parents baabb7d + e89dab4 commit 1a5eb21

File tree

3 files changed

+44
-21
lines changed

3 files changed

+44
-21
lines changed

examples/pxScene2d/src/v8_modules/http_test.js

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,27 +17,35 @@ limitations under the License.
1717
*/
1818

1919

20-
px.import({ scene: 'px:scene.1', http: 'http' }).then( function importsAreReady(imports)
20+
px.import({ scene: 'px:scene.1', http: 'http', url: 'url' }).then( function importsAreReady(imports)
2121
{
2222
var http = imports.http;
23-
24-
respData = '';
25-
26-
http.get('https://bellard.org/pi/pi2700e9', function (resp) {
27-
console.log('http in func cb');
28-
29-
resp.on('data', function (chunk) {
30-
console.log('http on data');
31-
respData += chunk;
23+
var url = imports.url;
24+
25+
var fetchCb = function (opt) {
26+
var respData = '';
27+
28+
http.get(opt, function (resp) {
29+
console.log('http in func cb');
30+
31+
resp.on('data', function (chunk) {
32+
console.log('http on data');
33+
respData += chunk;
34+
});
35+
36+
resp.on('end', function () {
37+
console.log('http done. status_code = ' + resp.statusCode
38+
+ ' response size = ' + respData.length);
39+
});
40+
}).on('error', function (e) {
41+
console.log('error fetching data: ' + e.message);
3242
});
43+
};
3344

34-
resp.on('end', function () {
35-
console.log('http done. status_code = ' + resp.statusCode
36-
+ ' response size = ' + respData.length);
37-
});
38-
}).on('error', function (e) {
39-
console.log('error fetching data: ' + e.message);
40-
});
45+
var fetchUrl = 'https://bellard.org/pi/pi2700e9';
46+
47+
fetchCb(fetchUrl);
48+
fetchCb(url.parse(fetchUrl));
4149

4250
}).catch( function importFailed(err){
4351
console.error("Import failed for http_test.js: " + err);

src/rtScriptV8/rtJsModulesV8.cpp

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -642,8 +642,23 @@ rtError rtHttpGetBinding(int numArgs, const rtValue* args, rtValue* result, void
642642
return RT_ERROR_INVALID_ARG;
643643
}
644644

645-
if (args[0].getType() != RT_stringType) {
646-
return RT_ERROR_INVALID_ARG;
645+
rtString resourceUrl;
646+
if (args[0].getType() == RT_stringType) {
647+
resourceUrl = args[0].toString();
648+
} else {
649+
if (args[0].getType() != RT_objectType) {
650+
return RT_ERROR_INVALID_ARG;
651+
}
652+
rtObjectRef obj = args[0].toObject();
653+
654+
rtString proto = obj.get<rtString>("protocol");
655+
rtString host = obj.get<rtString>("host");
656+
rtString path = obj.get<rtString>("path");
657+
658+
resourceUrl.append(proto.cString());
659+
resourceUrl.append("//");
660+
resourceUrl.append(host.cString());
661+
resourceUrl.append(path.cString());
647662
}
648663

649664
if (args[1].getType() != RT_functionType) {
@@ -654,7 +669,7 @@ rtError rtHttpGetBinding(int numArgs, const rtValue* args, rtValue* result, void
654669
rtObjectRef resp(new rtHttpResponse());
655670
args[1].toFunction().sendReturns(resp, ret);
656671

657-
rtFileDownloadRequest *downloadRequest = new rtFileDownloadRequest(args[0].toString(), resp.getPtr(), rtHttpResponse::onDownloadComplete);
672+
rtFileDownloadRequest *downloadRequest = new rtFileDownloadRequest(resourceUrl, resp.getPtr(), rtHttpResponse::onDownloadComplete);
658673
downloadRequest->setDownloadProgressCallbackFunction(rtHttpResponse::onDownloadInProgress, resp.getPtr());
659674
rtFileDownloader::instance()->addToDownloadQueue(downloadRequest);
660675

src/rtScriptV8/rtScriptV8.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,7 @@ rtV8Context::rtV8Context(Isolate *isolate, Platform *platform, uv_loop_t *loop)
471471
add("_testPromiseReturnRejectFunc", g_testPromiseReturnRejectFunc.getPtr());
472472
#endif
473473

474-
mHttpGetBinding = new rtFunctionCallback(rtHttpGetBinding);
474+
mHttpGetBinding = new rtFunctionCallback(rtHttpGetBinding, loop);
475475

476476
add("httpGet", mHttpGetBinding.getPtr());
477477
}

0 commit comments

Comments
 (0)