What is LdaKeyedProperty in V8 byte code? - javascript

This is a sample V8 byte code.
984 E> 00000318BA8792D3 # 41 : 29 02 07 LdaKeyedProperty a0, [7]

"Ld" means "load" (from memory)
"a" means "to the accumulator register" (as most bytecodes)
"KeyedProperty" means it's for JS code like obj[index] (as opposed to a "named property" like obj.foo).

Related

Why can't I type from 20 to 23 , but can type below 19 and it works fine ( input type - time )

<input type="time" />
On example above, you can see, when I type 19 - it replaces it to 07 pm, and it is correct, when I type 20, it converts it to 02 (and pm/am doesn't matter), is there any way to make it also works for range 20-23, when I type 20, it should convert it to 08 pm
This is the expected behavior for a time input on most browsers now. Typing in anything over 12 on a 12-hour clock will be interpreted as such. So "14" will return "02" and "19" will return "07".
The time input works according to the OS settings. For you and me, that's 12-hour. For much of the world, it's 24-hour. That's just how the input works, and you can read more about this on MDN.
Regarding a solution, I don't think this is something you can force change (i.e. against a user's default preferences) without using a different input type. Instead of time, you'd have to use text or numeric inputs and configure them to behave as a 24-hour clock.

Array of values saved in app.locals loses it's array format when used in view. What is happening here?

I am creating an array of date values and saving it in an app.locals variable as shown below:
prepDataList.forEach(function(item){
var str = item.createdAtDate.toDateString();
//str = str.substring(4, 10);
labelsArray.push(str);
countsArray.push(item.radarCount);
});
app.locals.chart1LabelsArray = labelsArray;
app.locals.chart1CountsArray = countsArray;
console.log("App locals labels array: ", app.locals.chart1LabelsArray);
The console.log() results are as follows:
App locals labels array: [ 'Thu Mar 26 2015',
'Fri Mar 27 2015',
'Sat Mar 28 2015',
'Sun Mar 29 2015',
'Mon Mar 30 2015' ]
So the app.locals.chart1LabelsArray variable seems to be stored correctly as an array.
Now, in my view (or rather in a script tag inside my view), if I call this variable, it doesn't seem to retain it's array format.
The type of the object is just [Object object].
I can't perform any functions such as forEach
The result doesn't seem to be a string either, since I could not run a .split(','); command on it.
The code:
console.log(<%= chart1LabelsArray %>);
Results in this error:
[Error] SyntaxError: Unexpected identifier 'Mar'. Expected ')' to end a argument list. (anonymous function)
I tried this with an array of integers as well, and seem to be plagued with the same issue.
When I inspect element in the browser, for the above line, here is what i see:
console.log(Thu Mar 26 2015,Fri Mar 27 2015,Sat Mar 28 2015,Sun Mar 29 2015,Mon Mar 30 2015);
So it seems that the app.locals variable I created in <%= chart1LabelsArray %> is getting pulled up, but not as an array. They come up as a continuous set of values, which don't seem to be accessible using any string or collection related functions.
Anyone know what I'm missing?
edit i didn't notice you were templating into javascript. the below paragraph is less relevant. all you need is to call JSON.stringify() when you template in the array.
all script has to be inside the lodash tags
<%= for(var i=0; i<chart1LabelsArray.length; i++){ %>
<span> label is <%= chart1LabelsArray[i] %></span>
<% } %>
PS, for anything more complex than a static list of data, I prefer to pass all data to clientside JS instead of manipulate the DOM in serverside templates. I especially dislike generating different scripts with a template on each request.
<body data-labels="<%= JSON.stringify(chart1LabelsArray) %>" ng-app="chartApp">
</body>
<script>
var labels = JSON.parse($('body').data(labels))
</script>

converting a JS Date to a Python date object

So I'm doing some Ajax trickery in the front page, and in the DJango backend, I send a JS Object, using AJAX...
the format is: 'Tue Jan 28 2014 00:00:00 GMT-0800 (PST)'
So I'm trying to convert it to a Python object:
import datetime
81 if request.is_ajax():
82 datestr = request.POST['from_date']
83 date = datetime.datetime.strptime(datestr, "%Y-%m-%dT%H:%M:%S.%fZ").date()
84 message = date.__str__()
85 else:
86 message = "Not Ajax"
87
88 return HttpResponse(message)
However I'm getting the following error:
time data 'Tue Jan 28 2014 00:00:00 GMT-0800 (PST)' does not match format '%Y-%m-%dT%H:%M:%S.%fZ'
How could I fix that?
I'm looking forward a nicer solution that would avoid splitting and parsing the string ...
Given the format in the error message, then the client you can use ES5s Date.prototype.toISOString() to convert a Date object to an ISO 8601 string. You'll need a polyfill for browsers that don't have it.

How can I parse Javascript variables using python?

The problem: A website I am trying to gather data from uses Javascript to produce a graph. I'd like to be able to pull the data that is being used in the graph, but I am not sure where to start. For example, the data might be as follows:
var line1=
[["Wed, 12 Jun 2013 01:00:00 +0000",22.4916114807,"2 sold"],
["Fri, 14 Jun 2013 01:00:00 +0000",27.4950008392,"2 sold"],
["Sun, 16 Jun 2013 01:00:00 +0000",19.5499992371,"1 sold"],
["Tue, 18 Jun 2013 01:00:00 +0000",17.25,"1 sold"],
["Sun, 23 Jun 2013 01:00:00 +0000",15.5420341492,"2 sold"],
["Thu, 27 Jun 2013 01:00:00 +0000",8.79045295715,"3 sold"],
["Fri, 28 Jun 2013 01:00:00 +0000",10,"1 sold"]];
This is pricing data (Date, Price, Volume). I've found another question here - Parsing variable data out of a js tag using python - which suggests that I use JSON and BeautifulSoup, but I am unsure how to apply it to this particular problem because the formatting is slightly different. In fact, in this problem the code looks more like python than any type of JSON dictionary format.
I suppose I could read it in as a string, and then use XPATH and some funky string editing to convert it, but this seems like too much work for something that is already formatted as a Javascript variable.
So, what can I do here to pull this type of organized data from this variable while using python? (I am most familiar with python and BS4)
If your format really is just one or more var foo = [JSON array or object literal];, you can just write a dotall regex to extract them, then parse each one as JSON. For example:
>>> j = '''var line1=
[["Wed, 12 Jun 2013 01:00:00 +0000",22.4916114807,"2 sold"],
["Fri, 14 Jun 2013 01:00:00 +0000",27.4950008392,"2 sold"],
["Sun, 16 Jun 2013 01:00:00 +0000",19.5499992371,"1 sold"],
["Tue, 18 Jun 2013 01:00:00 +0000",17.25,"1 sold"],
["Sun, 23 Jun 2013 01:00:00 +0000",15.5420341492,"2 sold"],
["Thu, 27 Jun 2013 01:00:00 +0000",8.79045295715,"3 sold"],
["Fri, 28 Jun 2013 01:00:00 +0000",10,"1 sold"]];\s*$'''
>>> values = re.findall(r'var.*?=\s*(.*?);', j, re.DOTALL | re.MULTILINE)
>>> for value in values:
... print(json.loads(value))
[[['Wed, 12 Jun 2013 01:00:00 +0000', 22.4916114807, '2 sold'],
['Fri, 14 Jun 2013 01:00:00 +0000', 27.4950008392, '2 sold'],
['Sun, 16 Jun 2013 01:00:00 +0000', 19.5499992371, '1 sold'],
['Tue, 18 Jun 2013 01:00:00 +0000', 17.25, '1 sold'],
['Sun, 23 Jun 2013 01:00:00 +0000', 15.5420341492, '2 sold'],
['Thu, 27 Jun 2013 01:00:00 +0000', 8.79045295715, '3 sold'],
['Fri, 28 Jun 2013 01:00:00 +0000', 10, '1 sold']]]
Of course this makes a few assumptions:
A semicolon at the end of the line must be an actual statement separator, not the middle of a string. This should be safe because JS doesn't have Python-style multiline strings.
The code actually does have semicolons at the end of each statement, even though they're optional in JS. Most JS code has those semicolons, but it obviously isn't guaranteed.
The array and object literals really are JSON-compatible. This definitely isn't guaranteed; for example, JS can use single-quoted strings, but JSON can't. But it does work for your example.
Your format really is this well-defined. For example, if there might be a statement like var line2 = [[1]] + line1; in the middle of your code, it's going to cause problems.
Note that if the data might contain JavaScript literals that aren't all valid JSON, but are all valid Python literals (which isn't likely, but isn't impossible, either), you can use ast.literal_eval on them instead of json.loads. But I wouldn't do that unless you know this is the case.
Okay, so there are a few ways to do it, but I ended up simply using a regular expression to find everything between line1= and ;
#Read page data as a string
pageData = sock.read()
#set p as regular expression
p = re.compile('(?<=line1=)(.*)(?=;)')
#find all instances of regular expression in pageData
parsed = p.findall(pageData)
#evaluate list as python code => turn into list in python
newParsed = eval(parsed[0])
Regex is nice when you have good coding, but is this method better (EDIT: or worse!) than any of the other answers here?
EDIT: I ultimately used the following:
#Read page data as a string
pageData = sock.read()
#set p as regular expression
p = re.compile('(?<=line1=)(.*)(?=;)')
#find all instances of regular expression in pageData
parsed = p.findall(pageData)
#load as JSON instead of using evaluate to prevent risky execution of unknown code
newParsed = json.loads(parsed[0])
The following makes a few assumptions such as knowing how the page is formatted, but a way of getting your example into memory on Python is like this
# example data
data = 'foo bar foo bar foo bar foo bar\r\nfoo bar foo bar foo bar foo bar \r\nvar line1=\r\n[["Wed, 12 Jun 2013 01:00:00 +0000",22.4916114807,"2 sold"],\r\n["Fri, 14 Jun 2013 01:00:00 +0000",27.4950008392,"2 sold"],\r\n["Sun, 16 Jun 2013 01:00:00 +0000",19.5499992371,"1 sold"],\r\n["Tue, 18 Jun 2013 01:00:00 +0000",17.25,"1 sold"],\r\n["Sun, 23 Jun 2013 01:00:00 +0000",15.5420341492,"2 sold"],\r\n["Thu, 27 Jun 2013 01:00:00 +0000",8.79045295715,"3 sold"],\r\n["Fri, 28 Jun 2013 01:00:00 +0000",10,"1 sold"]];\r\nfoo bar foo bar foo bar foo bar\r\nfoo bar foo bar foo bar foo bar'
# find your variable's start and end
x = data.find('line1=') + 6
y = data.find(';', x)
# so you can get just the relevant bit
interesting = data[x:y].strip()
# most dangerous step! don't do this on unknown sources
parsed = eval(interesting)
# maybe you'd want to use JSON instead, if the data has the right syntax
from json import loads as JSON
parsed = JSON(interesting)
# now parsed is your data
Assuming you have a python variable with a javascript line/block as a string like"var line1 = [[a,b,c], [d,e,f]];", you could use the following few lines of code.
>>> code = """var line1 = [['a','b','c'], ['d','e','f'], ['g','h','i']];"""
>>> python_readable_code = code.strip("var ;")
>>> exec(python_readable_code)
>>> print(line1)
[['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i']]
exec() Will run the code that is formatted as a string. In this case it will set the variable line1 to a list with lists.
And than you could use something like this:
for list in line1:
print(list[0], list[1], list[2])
# Or do something else with those values, like save them to a file

javascript Date().valueOf() is working on development PC but not on page served by server

The IE9 debugger (F12 developer tools -> script debugger) shows the following in the Locals window when I step through the code when the page is launched from the server:
midnight Fri Mar 15 00:00:00 EDT 2013 Object, (Date)
myDate Fri Mar 15 00:00:00 EDT 2013 Object, (Date)
and yet the following conditional test for equality of value resolves to false:
if (midnight.valueOf() === myDate.valueOf() ) {
// these lines of code are never reached
.
.
.
}
The odd thing, the === test resolves to true on my development PC. I cannot figure out why it resolves to false on the page served up by the server. The debugger clearly indicates it should resolve to true.
The document is in "IE9 Standards" mode.
The valueOf method returns the primitive value of a Date object as a number data type, the number of milliseconds since midnight 01 January, 1970 UTC.
The debugger only shows you the seconds so the variables may actually be different.

Categories