Skip to content

Commit

Permalink
Add plug-in authoring kit sources
Browse files Browse the repository at this point in the history
  • Loading branch information
CGJennings committed Sep 5, 2018
1 parent 45149d0 commit f1d4239
Show file tree
Hide file tree
Showing 284 changed files with 11,404 additions and 0 deletions.
271 changes: 271 additions & 0 deletions Plug-in Authoring Kit/Code Snippets and Demos/benchmarks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,271 @@
/*
* benchmarks.js - version 4
*
* This script runs some timed tests to gauge scripting
* performance. It also tests and demonstrates some of the
* new JavaScript 1.7 syntax that programmers may not be
* familiar with.
*
* If you wish to compare performance at different optimization
* levels, run this in Quickscript, then open preferences,
* change the optimizaton level, and run it again. Note that
* optimization levels 1 and 2 currently have the same effect
* (2 is reserved for possible future use).
*
* Note: depending on your system, running the tests may take
* a minute or more to complete. If you are feeling impatient,
* reduce the value of 'reps'. For more accurate results,
* increase it. Alternatively, reduce the number of iterations
* in each of the outermost loops by a factor of 10.
*/

importClass( java.lang.System );

/**
* time( func )
* Calls the function <tt>func</tt> with no arguments,
* and returns the (approximate) number of nanoseconds
* that the function took to execute.
*/
function time( func ) {
var start = System.nanoTime();
func();
var stop = System.nanoTime();
return stop-start;
}


// Each element in this array is a test function to be timed:
var tests = [

// use iteration to calc 99 factorial 10000 times
function iterative_factorial() {
for( var r=0; r<10000; ++r ) {
function fact( n ) {
var prod = 2;
for( var i=3; i<=n; ++i ) {
prod *= i;
}
}
fact( 99 );
}
},

// use tail recursion to calc factorial
function recursive_factorial() {
for( var r=0; r<10000; ++r ) {
function fact( n ) {
if( n == 2 ) return 2;
return n * fact(n-1);
}
fact( 99 );
}
},

// use yield to calc factorial
function generative_factorial() {
for( var r=0; r<10000; ++r ) {
function fact() {
var prod = 1, i = 2;
for( ;; ) {
yield prod *= i++;
}
}
var gen = fact();
for( var i=0; i<99; ++i ) {
gen.next();
}
}
},

// implement a Java interface 100 times and call each instance 1000 times
function interface_implementor() {
for( var r=0; r<100; ++r ) {
var listener = new java.awt.event.ActionListener() {
actionPerformed: function() { 1+1; }
};
for( var c=0; c<1000; ++c ) {
listener.actionPerformed(null);
}
}
},

// instantiate a JavaScript object with method 100 times and call each instance 1000 times
function function_caller() {
for( var r=0; r<100; ++r ) {
var listener = new Object() {
actionPerformed: function() { 1+1; }
};
for( var c=0; c<1000; ++c ) {
listener.actionPerformed(null);
}
}
},

// evaluate an expression with common subexpressions 200000 times
// optimization level 1 should eliminate common property access subexpressions
function common_subexpression() {
var x = new Object();
x.property = 1;
for( var i=0; i<200000; ++i ) {
x.property = (x.property + x.property) + (x.property + x.property) + x.property
+ (x.property + x.property) + (x.property + x.property) + x.property;
}
},

// convert a number from a JavaScript object to a Java object and back 50000 times
function object_conversion() {
var y = 0;
for( var i=0; i<50000; ++i ) {
var x = new java.lang.Integer(i&3);
y += x;
}
},

// swap two variables 500000 times using a temporary
function temp_swap() {
var a = 1;
var b = 2;
for( var i=0; i<500000; ++i ) {
var t = a;
a = b;
b = t;
}
},

// swap two variables 500000 times using destructuring assignment
function destructuring_swap() {
var a = 1;
var b = 2;
for( var i=0; i<500000; ++i ) {
[b, a] = [a, b];
}
},

// perform a string comparison 200000 times using JavaScript == operator
function native_compare() {
var s = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
var t = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXX.';
for( var i=0; i<200000; ++i ) {
s == t;
}
},

// perform a string comparison 200000 times using JavaScript method calls
function native_call() {
var s = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
var t = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXX.';
for( var i=0; i<200000; ++i ) {
s.equals( t );
}
},

// perform a string comparison 200000 times using Java String method calls
function java_call() {
var s = new java.lang.String( 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX' );
var t = new java.lang.String( 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXX.' );
for( var i=0; i<200000; ++i ) {
s.equals( t );
}
},

// read, unpack, modify, and write back each pixel of a 300x300
// image using getRGB/setRGB; the lesson of this is that if you
// need to manipulate an image you should use an image filter,
// preferably compiled, unless the image is very small
function unaccel_image_manip() {
var BI = java.awt.image.BufferedImage;
var w = 300, h = 300;
var im = new BI( w, h, BI.TYPE_INT_ARGB );
for( var y=0; y<h; ++y ) {
for( var x=0; x<w; ++x ) {
// get the pixel at (x,y) and unpack it to separate
// alpha, red, green, and blue components
var argb = im.getRGB( x, y );
var b = argb & 0xff;
var g = (argb>>8) & 0xff;
var r = (argb>>16) & 0xff;
var a = (argb>>24) & 0xff;
// invert the value of each component (0xff == 255)
a = 0xff-a;
r = 0xff-r;
g = 0xff-g;
b = 0xff-b;
// repack the components into a single value and replace
// the original image pixel
argb = (a<<24)|(r<<16)|(g<<8)|b;
im.setRGB( x, y, argb );
}
}
},

// solve an n-disk (currently 19) Tower of Hanoi puzzle

/////////////////////////////////
// CAUTION CAUTION CAUTION ///////////////////////////////////////
///////////////////////////////// //
// //
// Hanoi is an O(2^n) algorithm. It is easy to accidentally put in //
// a number of disks that would require longer than the age of the //
// universe to complete. Use caution and save your work. //
//////////////////////////////////////////////////////////////////////

function hanoi_puzzle() {
function move( from, to ) {
// move a disk from tower "from" to tower "to"
}

// solve a size n tower with the initial goal of
// moving all discs from tower "from" to tower "to",
// using tower "by" as a temporary workspace
function hanoi( n, from, to, by ) {
if( n == 1 ) {
move( from, to );
} else {
hanoi( n-1, from, by, to );
hanoi( 1, from, to, by );
hanoi( n-1, by , to, from );
}
}

hanoi( 19, 1, 2, 3 );
}

]; // END OF THE ARRAY OF TEST FUNCTIONS

/**
* runTests()
* Repeatedly times each function in <tt>tests</tt>,
* printing the name of each test and its best time.
*/
function runTests() {
var reps = 5;
for( var t=0; t<tests.length; ++t ) {
printf( '%25s ', tests[t].name );
var min = Number.MAX_VALUE;
for( var r=0; r<reps; ++r ) {
var dur = time( tests[t] );
min = Math.min( min, dur );
}
// shortest time is closest to theoretical best
printf( '%.0f ms\n', min * 1e-6 );
Console.flush();
}
printf( '%25s \n', 'ALL TESTS COMPLETED' );
}

Console.clear();
Console.visible = true;

// Run in event thread:
// (this may give more accurate results but SE will freeze until
// the tests complete)

//runTests();

// OR

// Run in background thread:

useLibrary( 'threads' );
Thread.run( runTests );
97 changes: 97 additions & 0 deletions Plug-in Authoring Kit/Code Snippets and Demos/busy-window.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
* busy-window.js - version 1
*
* Demonstrates the use of Thread.busyWindow.
*/

useLibrary( 'threads' );

/*
Thread.busyWindow allows you to perform a long-running
task while providing feedback to the user on the current
progress. To use Thread.busyWindow, you pass a function
to it that performs the desired task. By default, an
'indeterminate' progress bar and default message will be
displayed:
*/

function task1() {
// simulate doing a lot of work
sleep( 1500 );
}

Thread.busyWindow( task1 );



/*
A 'callback' object is passed to your task function.
This object has several write-only properties that
give you more control over the window that appears.
The currentProgress and maximumProgress values allow
you to provide feedback on the progress being made.
*/

function task2( callback ) {
callback.maximumProgress = 15;
for( var i=0; i<=15; ++i ) {
callback.currentProgress = i;
sleep( 100 );
}
}

Thread.busyWindow( task2, 'Making Progress...' );



/*
You can also update the title and display a status message
using the callback.
*/

function task3( callback ) {
callback.maximumProgress = 100;
for( var i=0; i<=100; ++i ) {
if( i == 50 ) {
callback.title = 'Phase II';
}
callback.currentProgress = i;
callback.status = 'Step ' + i, 100;
sleep( 50 );
}
}

Thread.busyWindow( task3, 'Phase I' );



/*
Finally, if you call busyWindow with a value of true
for the third parameter, then the window will have a
Cancel button. To check if the user has cancelled
the operation, use the callback object's cancelled
property. If it is true, the operation has been cancelled
and you should stop the operation at the earliest
opportunity.
*/

function task4( callback ) {
callback.maximumProgress = 1000;
for( var i=0; i<=1000; ++i ) {
// quit if the user presses Cancel
if( callback.cancelled ) return;
callback.currentProgress = i;
sleep( 10 );
}
}

Thread.busyWindow( task4, 'Lengthy Task...', true );


/*
Note: Keep in mind that the task is run outside of the
event dispatch thread. If you want to update any user
interface features from the task thread, you *must* use
invokeLater or invokeAndWait.
*/
Loading

0 comments on commit f1d4239

Please sign in to comment.