From a7592250f32e7a54ab4991a40610c56958ce0692 Mon Sep 17 00:00:00 2001 From: danielyxie Date: Thu, 27 Sep 2018 23:19:52 -0500 Subject: [PATCH] Finished wget() Netscript fn implementation. Updated documentation --- doc/source/netscriptfunctions.rst | 13 ++++++++- doc/source/netscriptjs.rst | 1 + src/Constants.js | 1 - src/NetscriptFunctions.js | 47 +++++++++++++++---------------- src/Script.js | 7 ++--- 5 files changed, 39 insertions(+), 30 deletions(-) diff --git a/doc/source/netscriptfunctions.rst b/doc/source/netscriptfunctions.rst index 25086bf05..43343da74 100644 --- a/doc/source/netscriptfunctions.rst +++ b/doc/source/netscriptfunctions.rst @@ -1041,7 +1041,18 @@ wget wget("https://raw.githubusercontent.com/danielyxie/bitburner/master/README.md", "game_readme.txt"); - Returns a boolean indicating whether or not the data was successfully downloaded. + **IMPORTANT:** This is an asynchronous function that returns a Promise. The Promise's resolved + value will be a boolean indicating whether or not the data was successfully + retrieved from the URL. Because the function is async and returns a Promise, + it is recommended you use :code:`wget` in :ref:`netscriptjs`. + + In NetscriptJS, you must preface any call to + :code:`wget` with the :code:`await` keyword (like you would :code:`hack` or :code:`sleep`). + + :code:`wget` will still work in :ref:`netscript1`, but the functions execution will not + be synchronous (i.e. it may not execute when you expect/want it to). Furthermore, since Promises are not + supported in ES5, you will not be able to process the returned value of :code:`wget` in + Netscript 1.0. getFavorToDonate ^^^^^^^^^^^^^^^^ diff --git a/doc/source/netscriptjs.rst b/doc/source/netscriptjs.rst index 2fdf6df7c..8e0f2eff1 100644 --- a/doc/source/netscriptjs.rst +++ b/doc/source/netscriptjs.rst @@ -58,6 +58,7 @@ Here is a summary of all rules you need to follow when writing Netscript JS code * run * exec * prompt + * wget * Any function that contains :code:`await` must be declared as :code:`async` diff --git a/src/Constants.js b/src/Constants.js index fec913152..88a06b68d 100644 --- a/src/Constants.js +++ b/src/Constants.js @@ -85,7 +85,6 @@ let CONSTANTS = { ScriptGetScriptRamCost: 0.1, ScriptGetHackTimeRamCost: 0.05, ScriptGetFavorToDonate: 0.10, - ScriptWgetRamCost: 5, ScriptCodingContractBaseRamCost:10, ScriptSingularityFn1RamCost: 1, diff --git a/src/NetscriptFunctions.js b/src/NetscriptFunctions.js index 286659564..d22edc0fd 100644 --- a/src/NetscriptFunctions.js +++ b/src/NetscriptFunctions.js @@ -2262,35 +2262,34 @@ function NetscriptFunctions(workerScript) { }); }, wget : async function(url, target, ip=workerScript.serverIp) { - if (workerScript.checkingRam) { - return updateStaticRam("wget", CONSTANTS.ScriptWgetRamCost); - } - updateDynamicRam("wget", CONSTANTS.ScriptWgetRamCost); + if (workerScript.checkingRam) { return 0; } if (!isScriptFilename(target) && !target.endsWith(".txt")) { workerSript.log(`ERROR: wget() failed because of an invalid target file: ${target}. Target file must be a script or text file`); return false; } var s = safeGetServer(ip, "wget"); - $.get(url, function(data) { - let res; - if (isScriptFilename(target)) { - res = s.writeToScriptFile(target, data); - } else { - res = s.writeToTextFile(target, data); - } - if (!res.success) { - workerScript.log("ERROR: wget() failed"); - return false; - } - if (res.overwritten) { - workerScript.log(`wget() successfully retrieved content and overwrote ${target} on ${ip}`); - return true; - } - workerScript.log(`wget successfully retrieved content to new file ${target} on ${ip}`); - return true; - }, 'text').fail(function(e) { - workerScript.log("ERROR: wget() failed: " + JSON.stringify(e)); - return false; + return new Promise(function(resolve, reject) { + $.get(url, function(data) { + let res; + if (isScriptFilename(target)) { + res = s.writeToScriptFile(target, data); + } else { + res = s.writeToTextFile(target, data); + } + if (!res.success) { + workerScript.log("ERROR: wget() failed"); + return resolve(false); + } + if (res.overwritten) { + workerScript.log(`wget() successfully retrieved content and overwrote ${target} on ${ip}`); + return resolve(true); + } + workerScript.log(`wget successfully retrieved content to new file ${target} on ${ip}`); + return resolve(true); + }, 'text').fail(function(e) { + workerScript.log(`ERROR: wget() failed: ${JSON.stringify(e)}`); + return resolve(false) + }); }); }, getFavorToDonate: function() { diff --git a/src/Script.js b/src/Script.js index 34d01d8ec..997fb6729 100755 --- a/src/Script.js +++ b/src/Script.js @@ -518,12 +518,11 @@ function parseOnlyRamCalculate(server, code, workerScript) { if (typeof func === "function") { try { let res; - if (func instance of (async () => {}).constructor) { - + if (func.constructor.name === "AsyncFunction") { + res = 0; // Async functions will always be 0 RAM } else { - + res = func.apply(null, []); } - let res = func.apply(null, []); if (typeof res === "number") { return res; }