Perlito5
view release on metacpan or search on metacpan
lib/Perlito5/JavaScript2/CORE.pm view on Meta::CPAN
use v5;
package Perlito5::JavaScript2::CORE;
sub emit_javascript2 {
return <<'EOT'
//
//
// lib/Perlito5/JavaScript2/CORE.js
//
// CORE functions for "Perlito" Perl5-in-JavaScript2
//
// AUTHORS
//
// Flavio Soibelmann Glock fglock@gmail.com
//
// COPYRIGHT
//
// Copyright 2009, 2010, 2011, 2012 by Flavio Soibelmann Glock and others.
//
// This program is free software; you can redistribute it and/or modify it
// under the same terms as Perl itself.
//
// See http://www.perl.com/perl/misc/Artistic.html
var CORE = p5pkg.CORE;
var isNode = typeof require != "undefined";
EOT
# sleep()
. <<'EOT'
if (isNode) {
try {
var sleep = require("sleep");
CORE.sleep = function(List__) {
var n = p5num(List__[0]) || 1;
sleep.usleep(n * 1000000); // sleep for n seconds (1 second is 1000000 microseconds)
return n;
}
}
catch (err) {
CORE.sleep = function(List__) {
CORE.die("sleep() function failed. Maybe you need 'npm install sleep'?\n" + err);
}
}
}
if (!CORE.sleep) {
CORE.sleep = function(List__) {
CORE.die("sleep() not supported for this platform");
}
}
EOT
# crypt()
. <<'EOT'
if (isNode) {
try {
var crypt = require("crypt3");
CORE.crypt = function(List__) {
var text = p5str(List__[0]);
var salt = p5str(List__[1]);
while(salt.length < 2) {
salt += "A";
}
return crypt(text, salt);
}
}
catch (err) {
CORE.crypt = function(List__) {
CORE.die("crypt() function failed. Maybe you need 'npm install crypt3'?\n" + err);
}
}
}
if (!CORE.crypt) {
CORE.crypt = function(List__) {
CORE.die("crypt() not supported for this platform");
}
}
EOT
# time()
. <<'EOT'
CORE.time = function(List__) {
return CORE.int([Date.now() / 1000]);
}
EOT
#
# Note: gmtime / localtime test:
#
# $ node perlito5.js -I src5/lib -e ' print gmtime . " @{[ gmtime ]}\n" . localtime . " @{[ localtime ]}\n" ' ; perl -e ' print gmtime . " @{[ gmtime ]}\n" . localtime . " @{[ localtime ]}\n" '
#
# TODO - isdst is not implemented
# See:
# http://stackoverflow.com/questions/11887934/check-if-daylight-saving-time-is-in-effect-and-if-it-is-for-how-many-hours
#
. <<'EOT'
var _fmt_date = function(date) {
return ['Sun','Mon','Tue','Wed','Thu','Fri','Sat'][date.getDay()] + ' ' +
['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'][date.getMonth()] + ' ' +
date.getDate() + ' ' +
CORE.sprintf([ "%02d:%02d:%02d ", date.getHours(), date.getMinutes(), date.getSeconds() ]) +
date.getFullYear();
}
var _list_date = function(date) {
var year_start = new Date(date);
year_start.setMonth(0, 1);
var year_day = Math.round((date-year_start)/8.64e7);
var isdst = 0; // not implemented
return [date.getSeconds(),date.getMinutes(),date.getHours(),date.getDate(),
date.getMonth(),date.getFullYear()-1900,date.getDay(),
year_day,
isdst
( run in 0.527 second using v1.01-cache-2.11-cpan-39bf76dae61 )