How to modify an actionscript function and execute it in a webpage? - actionscript

I have an actionscript function that, after modifying some values and "executing" it, it has to return some specific result, my question is:
Is there a way to build a webpage that let users modify that function using a form and then execute it in order to get a result?
How do I "execute" actionscript functions? Thanks!

When you say modifying a function, do you want the user to create a function on the fly, and allow him to execute it?
Or you simply want to pass some arguments to an already existing function and then do some calculation and return the result?
Incase you simply want to execute a function within actionscript, from javascript, you can use ExternalInterface in ActionScript 3.0 , define a function and allow javascript to call that function.
public function functionToBeCalledFromJS(argument1,argument2)
{
Alert.show(argument1);
}
ExternalInterface.addCallBack('ASfunction',functionToBeCalledFromJS);
In JS when user submits a form call:
ASfunction(argument1,argument2);
Incase you want to return some data from AS to JS, then you need to call a JS function from within AS.
This can be easily accomplished using ExternalInterface.call();
Eg.
Lets say, you needed to do some calculation and then return the data
public function functionToBeCalledFromJS(argument1,argument2)
{
var returnInt:int = argument1+argument2;
}
ExternalInterface.call('JSFunction',returnInt);
IN JS:
function JSFunction(result)
{
alert(result);
}
For further details on this, you can go to http://www.adobe.com/livedocs/flash/9.0/ActionScriptLangRefV3/flash/external/ExternalInterface.html#addCallback%28%29, which is the official site on AS3 reference.

Related

Is it possible in Lua to bind an argument to a callback function passed as argument to another function? (JavaScript's "bind" equivalent)

I have a third party API that has an event listener adding function which takes as parameter a callback function to be triggered when the event occurs. I would like to pass argument to that callback function. I'm looking for Lua's equivalent of JavaScript's bind.
The Lua code:
EventListenerAddingFunction(myCallbackFunction); // I want to add a param to the callback here
How I would do it in JS:
EventListenerAddingFunction(myCallbackFunction.bind({}, myParameter));
Can this be done in Lua?
No Lua doesn't have this feature, so closest I can think of would be making a closure-wrapper:
EventListenerAddingFunction(function(...) myCallbackFunction({}, myParameter, ...) end)
This passes your parameter everytime the callback is called, all other callback parameters will be passed next. If you don't know your parameters use ... (I don't know them so I used varargs), it's better if you pass exact amount of parameters.

Jira java variable inside velocity in javascript

I am developing jira and using variables in velocity, which are exposed as public getters in java plugin code. Everything seems good, I am getting my java code results in output.vm inside velocity template after processing excel file but I would like to add progressbar which is coded in pure javascript, so I have some js files and I would like to get velocity variables like current number of processed issue, and
how many issues are there to calculate progress. I can't bring solution because when
I am attaching <script> inside velocity .vm and alerting after timeouted function
the variable issueNumber is not changing at all, its 0. issueNumber is public instance variable in java.
<script>
function test() {
var get = $issueNumber;
alert(get);
}
setTimeout(test, 5000);
test();
</script>
My public variable issueNumber can be rendered in velocity as result after iterating in output velocity file, but can't be used as indicator in javascript where current progress is. Basically I need runtime variable not render time variable.
The velocity engine just replaces $issueNumber with the constant value during html page rendering and it happens only once so it is impossible to track any changes in that way. To achieve what you want you can implement an endpoint on the server-side which returns the actual state of the progress and do ajax calls to it in your script with some interval.

Lua - Getting a Localized Variable or Function without using the Debug Library with 2 seperate scripts

Ok so I am using a object orientated environment, I can make several scripts that may or may not connect with each other, all depending on if I want them to connect or not, and I want to get a function that is localized local myfunc = function() end from a different object that can handle code, from this point forward I will be calling these objects "Scripts" as they are what they are called in the game and are easily used for me to tell people what I am talking about even if they are not formally used as a name to suggest such a thing.
So lets say I have Script 1 with this code:
local myfunc = function() return true end
and I have Script 2 with a blank sheet, I want to make it so I can get myfunc without touching the debug library, making the original script a module script and returning the function, and this has to stay in 2 separate scripts. That is all for the requirements if you are wondering. I expect this can be done and I hope someone out there has the knowledge on how to do something like this clean and efficiently!
The whole point of a local variable is that it's local; other people can't touch it. The traditional means of having one script access data from another are modules or global variables. Both options which you have declared that you can't/won't do.
Your requirements reduce the set of possible solutions to zero.
Lua chunks can have return statements. To return a single function:
return function()
return true
end
To return a table with multiple functions:
return {
myFunc = function()
return true
end,
myOtherFunc = function()
return false
end,
}

How to run multiple functions onClick

The reason it took me forever to find out is that I don't know how it is called. But I hope if I describe the question here as thoroughly as possible, it will pop up in the search results anyways.
Possible other titles:
How can I pass a function without name as parameter to querySelector.onClick.listen?
How to use a function without a name in dart?
How to create a nameless function that you can use only exactly there where it is defined?
I know that the following works - also hosted on dartpad
void main(){ querySelector('#btn').onClick.listen((e)=>fnct()); }
void fnct(){ querySelector('#btn').text="hola"; print("test");}
This changes the text of the button to "hola" and prints "test".
But what if I don't want to define a new function just for this because I like to keep the flow when reading the code and don't like to jump from function to function needlessly?
After an hour of searching, I found this
For my own code example, it would be like this dartpad link:
void main(){
querySelector('#btn').onClick.listen((e){
querySelector('#btn').text="hello";
print("no hablo espanol");
});
}
So you can define a function on the flow by using
(param){command(); secondCommand(param);}
It is entirely possible that you can find this somewhere. But I did not with my search terms. So if any of you know what the correct search terms would have been, let me know :)

how to synchronize methods in actionscript?

The question is how could I stop a method being called twice, where the first call has not "completed" because its handler is waiting for a url to load for example?
Here is the situation:
I have written a flash client which interfaces with a java server using a binary encrypted protocol (I would love to not have had to re-invent the whole client/server object communcation stack, but I had to encrypt the data in such a way that simple tools like tamper data and charles proxy could not pick them up if using SSL).
The API presents itself to flas as an actionscript swf file, and the API itself is a singleton.
the api exposes some simple methods, including:
login()
getBalance()
startGame()
endGame()
Each method will call my HttpCommunicator class.
HttpCommunicator.as (with error handling and stuff removed):
public class HttpCommunicator {
private var _externalHalder:function;
public function communicate(data:String, externalHandler:APIHandler):void {
// do encryption
// add message numbers etc to data.
this._externalHalder = externalHandler;
request.data = encrypt(addMessageNumers(data)));
loader.addEventListener(Event.COMPLETE, handleComplete);
loader.load(request);
}
private function handleComplete(event:Event):void {
var loader:URLLoader = URLLoader(event.target);
String data = decrypt(loader.data);
// check message numbers match etc.
_externalHandler(data);
}
The problem with this is I cant protect the same HttpCommunicator object from being called twice before the first has handled the complete event, unless:
I create a new HttpCommunicator object every single time I want to send a message. I also want to avoid creating a URLLoader each time, but this is not my code so will be more problematic to know how it behaves).
I can do something like syncronize on communicate. This would effectivly block, but this is better than currupting the data transmission. In theory, the Flash client should not call the same api function twice in a row, but I but it will happen.
I implement a queue of messages. However, this also needs syncronization around the push and pop methods, which I cant find how to do.
Will option 1. even work? If I have a singleton with a method say getBalance, and the getBalance method has:
// class is instantiated through a factory as a singleton
public class API{
var balanceCommunicator:HttpCommunicator = new HttpCommunicator(); // create one for all future calls.
public funciton getBalance(playerId:uint, hander:Fuction):Number {
balanceCommunicator.communicate(...); // this doesnt block
// do other stuff
}
Will the second call trounce the first calls communicator variable? i.e. will it behave as if its static, as there is onlyone copy of the API object?
If say there was a button on the GUI which had "update balance", and the user kept clicking on it, at the same time as say a URLLoader complete event hander being called which also cals the apis getBalance() function (i.e. flash being multithreaded).
Well, first off, with the exception of the networking APIs, Flash is not multithreaded. All ActionScript runs in the same one thread.
You could fairly easily create a semaphore-like system where each call to communicate passed in a "key" as well as the arguments you already specified. That "key" would just be a string that represented the type of call you're doing (getBalance, login, etc). The "key" would be a property in a generic object (Object or Dictionary) and would reference an array (it would have to be created if it didn't exist).
If the array was empty then the call would happen as normal. If not then the information about the call would be placed into an object and pushed into the array. Your complete handler would then have to just check, after it finished a call, if there were more requests in the queue and if so dequeue one of them and run that request.
One thing about this system would be that it would still allow different types of requests to happen in parallel - but you would have to have a new URLLoader per request (which is perfectly reasonable as long as you clean it up after each request is done).

Resources