flat assembler
Message board for the users of flat assembler.

Index > Heap > Is JavaScript broken, really?

Author
Thread Post new topic Reply to topic
baldr



Joined: 19 Mar 2008
Posts: 1651
baldr
Hi there!

Lately I was thinking about the way JS goes (you know, all that kludges, "use strict" and such). Is it only me, or it's really becomes pretty mess of 'implementation defined' behavior?

Then I opt to something cleanlier, like Lua.
Post 09 Aug 2012, 03:38
View user's profile Send private message Reply with quote
typedef



Joined: 25 Jul 2010
Posts: 2913
Location: 0x77760000
typedef
I haven't tried Lua but I hear it's good and easy.

My question is, is it OOP? If not, then it's different to JS.
Post 09 Aug 2012, 22:26
View user's profile Send private message Reply with quote
baldr



Joined: 19 Mar 2008
Posts: 1651
baldr
typedef,

It can be. Roberto Ierusalimschy's "Programming in Lua" explains how.
Post 10 Aug 2012, 08:30
View user's profile Send private message Reply with quote
TmX



Joined: 02 Mar 2006
Posts: 821
Location: Jakarta, Indonesia
TmX
Here's a comparison of Lua and JS (not sure how update it is):
http://phrogz.net/lua/LearningLua_FromJS.html

If you're interested in Lua, also have a look at LuaJIT
Post 10 Aug 2012, 15:05
View user's profile Send private message Reply with quote
Enko



Joined: 03 Apr 2007
Posts: 678
Location: Mar del Plata
Enko
And what about that HTML5+Javascript is slower then Flash with Actionscript for Games?
At least i tried some html5 games and the performance wasn't so good.


Actionscript is a good script language, specialy the latest 3.0 version with good oop implementation
http://www.adobe.com/devnet/actionscript/articles/oop_as3.html

Edited:
Wow, Adobe actually have a greate documentation like this pdf
http://wwwimages.adobe.com/www.adobe.com/content/dam/Adobe/en/devnet/actionscript/pdfs/ora_learning_as3_ch07.pdf
Post 11 Aug 2012, 02:45
View user's profile Send private message Reply with quote
Tyler



Joined: 19 Nov 2009
Posts: 1216
Location: NC, USA
Tyler
If only Flash was open. I hope it dies, if it stays in the same closed state it's in. There's no reason some LLVM JIT (or Java) based technology couldn't easily replace it with similar performance and equal portability, with the bonus that they are free technologies that can't be denied to whole user groups (like Adobe is doing to Linux).
Post 11 Aug 2012, 18:27
View user's profile Send private message Reply with quote
f0dder



Joined: 19 Feb 2004
Posts: 3170
Location: Denmark
f0dder
Hmm, Java... "free technology"?

*cough* whOracle *cough*. And don't forget that crApple in a major influx of cash to the LLVM project (they don't like the GPL license, so they're doing what they can to move their GCC toolchain over to Clang).
Post 12 Aug 2012, 00:27
View user's profile Send private message Visit poster's website Reply with quote
Enko



Joined: 03 Apr 2007
Posts: 678
Location: Mar del Plata
Enko
And what about Google Go Lang?
http://golang.org/doc/go_faq.html

hello world with windclass and all the includes necesary.

Executable size: 1.5xB
Code:
// Copyright 2011 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// +build windows

package main

import (
        "fmt"
        "os"
        "syscall"
        "unsafe"
)

type Wndclassex struct {
        Size       uint32
        Style      uint32
        WndProc    uintptr
        ClsExtra   int32
        WndExtra   int32
        Instance   syscall.Handle
        Icon       syscall.Handle
        Cursor     syscall.Handle
        Background syscall.Handle
        MenuName   *uint16
        ClassName  *uint16
        IconSm     syscall.Handle
}

type Point struct {
        X uintptr
        Y uintptr
}

type Msg struct {
        Hwnd    syscall.Handle
        Message uint32
        Wparam  uintptr
        Lparam  uintptr
        Time    uint32
        Pt      Point
}

const (
        // Window styles
        WS_OVERLAPPED   = 0
        WS_POPUP        = 0x80000000
        WS_CHILD        = 0x40000000
        WS_MINIMIZE     = 0x20000000
        WS_VISIBLE      = 0x10000000
        WS_DISABLED     = 0x8000000
        WS_CLIPSIBLINGS = 0x4000000
        WS_CLIPCHILDREN = 0x2000000
        WS_MAXIMIZE     = 0x1000000
        WS_CAPTION      = WS_BORDER | WS_DLGFRAME
        WS_BORDER       = 0x800000
        WS_DLGFRAME     = 0x400000
        WS_VSCROLL      = 0x200000
        WS_HSCROLL      = 0x100000
        WS_SYSMENU      = 0x80000
        WS_THICKFRAME   = 0x40000
        WS_GROUP        = 0x20000
        WS_TABSTOP      = 0x10000
        WS_MINIMIZEBOX  = 0x20000
        WS_MAXIMIZEBOX  = 0x10000
        WS_TILED        = WS_OVERLAPPED
        WS_ICONIC       = WS_MINIMIZE
        WS_SIZEBOX      = WS_THICKFRAME
        // Common Window Styles
        WS_OVERLAPPEDWINDOW = WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX
        WS_TILEDWINDOW      = WS_OVERLAPPEDWINDOW
        WS_POPUPWINDOW      = WS_POPUP | WS_BORDER | WS_SYSMENU
        WS_CHILDWINDOW      = WS_CHILD

        WS_EX_CLIENTEDGE = 0x200

        // Some windows messages
        WM_CREATE  = 1
        WM_DESTROY = 2
        WM_CLOSE   = 16
        WM_COMMAND = 273

        // Some button control styles
        BS_DEFPUSHBUTTON = 1

        // Some color constants
        COLOR_WINDOW  = 5
        COLOR_BTNFACE = 15

        // Default window position
        CW_USEDEFAULT = 0x80000000 - 0x100000000

        // Show window default style
        SW_SHOWDEFAULT = 10
)

var (
        // Some globally known cursors
        IDC_ARROW = MakeIntResource(32512)
        IDC_IBEAM = MakeIntResource(32513)
        IDC_WAIT  = MakeIntResource(32514)
        IDC_CROSS = MakeIntResource(32515)

        // Some globally known icons
        IDI_APPLICATION = MakeIntResource(32512)
        IDI_HAND        = MakeIntResource(32513)
        IDI_QUESTION    = MakeIntResource(32514)
        IDI_EXCLAMATION = MakeIntResource(32515)
        IDI_ASTERISK    = MakeIntResource(32516)
        IDI_WINLOGO     = MakeIntResource(32517)
        IDI_WARNING     = IDI_EXCLAMATION
        IDI_ERROR       = IDI_HAND
        IDI_INFORMATION = IDI_ASTERISK
)

//sys   GetModuleHandle(modname *uint16) (handle syscall.Handle, err error) = GetModuleHandleW
//sys   RegisterClassEx(wndclass *Wndclassex) (atom uint16, err error) = user32.RegisterClassExW
//sys   CreateWindowEx(exstyle uint32, classname *uint16, windowname *uint16, style uint32, x int32, y int32, width int32, height int32, wndparent syscall.Handle, menu syscall.Handle, instance syscall.Handle, param uintptr) (hwnd syscall.Handle, err error) = user32.CreateWindowExW
//sys   DefWindowProc(hwnd syscall.Handle, msg uint32, wparam uintptr, lparam uintptr) (lresult uintptr) = user32.DefWindowProcW
//sys   DestroyWindow(hwnd syscall.Handle) (err error) = user32.DestroyWindow
//sys   PostQuitMessage(exitcode int32) = user32.PostQuitMessage
//sys   ShowWindow(hwnd syscall.Handle, cmdshow int32) (wasvisible bool) = user32.ShowWindow
//sys   UpdateWindow(hwnd syscall.Handle) (err error) = user32.UpdateWindow
//sys   GetMessage(msg *Msg, hwnd syscall.Handle, MsgFilterMin uint32, MsgFilterMax uint32) (ret int32, err error) [failretval==-1] = user32.GetMessageW
//sys   TranslateMessage(msg *Msg) (done bool) = user32.TranslateMessage
//sys   DispatchMessage(msg *Msg) (ret int32) = user32.DispatchMessageW
//sys   LoadIcon(instance syscall.Handle, iconname *uint16) (icon syscall.Handle, err error) = user32.LoadIconW
//sys   LoadCursor(instance syscall.Handle, cursorname *uint16) (cursor syscall.Handle, err error) = user32.LoadCursorW
//sys   SetCursor(cursor syscall.Handle) (precursor syscall.Handle, err error) = user32.SetCursor
//sys   SendMessage(hwnd syscall.Handle, msg uint32, wparam uintptr, lparam uintptr) (lresult uintptr) = user32.SendMessageW
//sys   PostMessage(hwnd syscall.Handle, msg uint32, wparam uintptr, lparam uintptr) (err error) = user32.PostMessageW

func MakeIntResource(id uint16) *uint16 {
        return (*uint16)(unsafe.Pointer(uintptr(id)))
}


var (
        modkernel32 = syscall.NewLazyDLL("kernel32.dll")
        moduser32   = syscall.NewLazyDLL("user32.dll")

        procGetModuleHandleW = modkernel32.NewProc("GetModuleHandleW")
        procRegisterClassExW = moduser32.NewProc("RegisterClassExW")
        procCreateWindowExW  = moduser32.NewProc("CreateWindowExW")
        procDefWindowProcW   = moduser32.NewProc("DefWindowProcW")
        procDestroyWindow    = moduser32.NewProc("DestroyWindow")
        procPostQuitMessage  = moduser32.NewProc("PostQuitMessage")
        procShowWindow       = moduser32.NewProc("ShowWindow")
        procUpdateWindow     = moduser32.NewProc("UpdateWindow")
        procGetMessageW      = moduser32.NewProc("GetMessageW")
        procTranslateMessage = moduser32.NewProc("TranslateMessage")
        procDispatchMessageW = moduser32.NewProc("DispatchMessageW")
        procLoadIconW        = moduser32.NewProc("LoadIconW")
        procLoadCursorW      = moduser32.NewProc("LoadCursorW")
        procSetCursor        = moduser32.NewProc("SetCursor")
        procSendMessageW     = moduser32.NewProc("SendMessageW")
        procPostMessageW     = moduser32.NewProc("PostMessageW")
)

func GetModuleHandle(modname *uint16) (handle syscall.Handle, err error) {
        r0, _, e1 := syscall.Syscall(procGetModuleHandleW.Addr(), 1, uintptr(unsafe.Pointer(modname)), 0, 0)
        handle = syscall.Handle(r0)
        if handle == 0 {
                if e1 != 0 {
                        err = error(e1)
                } else {
                        err = syscall.EINVAL
                }
        }
        return
}

func RegisterClassEx(wndclass *Wndclassex) (atom uint16, err error) {
        r0, _, e1 := syscall.Syscall(procRegisterClassExW.Addr(), 1, uintptr(unsafe.Pointer(wndclass)), 0, 0)
        atom = uint16(r0)
        if atom == 0 {
                if e1 != 0 {
                        err = error(e1)
                } else {
                        err = syscall.EINVAL
                }
        }
        return
}

func CreateWindowEx(exstyle uint32, classname *uint16, windowname *uint16, style uint32, x int32, y int32, width int32, height int32, wndparent syscall.Handle, menu syscall.Handle, instance syscall.Handle, param uintptr) (hwnd syscall.Handle, err error) {
        r0, _, e1 := syscall.Syscall12(procCreateWindowExW.Addr(), 12, uintptr(exstyle), uintptr(unsafe.Pointer(classname)), uintptr(unsafe.Pointer(windowname)), uintptr(style), uintptr(x), uintptr(y), uintptr(width), uintptr(height), uintptr(wndparent), uintptr(menu), uintptr(instance), uintptr(param))
        hwnd = syscall.Handle(r0)
        if hwnd == 0 {
                if e1 != 0 {
                        err = error(e1)
                } else {
                        err = syscall.EINVAL
                }
        }
        return
}

func DefWindowProc(hwnd syscall.Handle, msg uint32, wparam uintptr, lparam uintptr) (lresult uintptr) {
        r0, _, _ := syscall.Syscall6(procDefWindowProcW.Addr(), 4, uintptr(hwnd), uintptr(msg), uintptr(wparam), uintptr(lparam), 0, 0)
        lresult = uintptr(r0)
        return
}

func DestroyWindow(hwnd syscall.Handle) (err error) {
        r1, _, e1 := syscall.Syscall(procDestroyWindow.Addr(), 1, uintptr(hwnd), 0, 0)
        if int(r1) == 0 {
                if e1 != 0 {
                        err = error(e1)
                } else {
                        err = syscall.EINVAL
                }
        }
        return
}

func PostQuitMessage(exitcode int32) {
        syscall.Syscall(procPostQuitMessage.Addr(), 1, uintptr(exitcode), 0, 0)
        return
}

func ShowWindow(hwnd syscall.Handle, cmdshow int32) (wasvisible bool) {
        r0, _, _ := syscall.Syscall(procShowWindow.Addr(), 2, uintptr(hwnd), uintptr(cmdshow), 0)
        wasvisible = bool(r0 != 0)
        return
}

func UpdateWindow(hwnd syscall.Handle) (err error) {
        r1, _, e1 := syscall.Syscall(procUpdateWindow.Addr(), 1, uintptr(hwnd), 0, 0)
        if int(r1) == 0 {
                if e1 != 0 {
                        err = error(e1)
                } else {
                        err = syscall.EINVAL
                }
        }
        return
}

func GetMessage(msg *Msg, hwnd syscall.Handle, MsgFilterMin uint32, MsgFilterMax uint32) (ret int32, err error) {
        r0, _, e1 := syscall.Syscall6(procGetMessageW.Addr(), 4, uintptr(unsafe.Pointer(msg)), uintptr(hwnd), uintptr(MsgFilterMin), uintptr(MsgFilterMax), 0, 0)
        ret = int32(r0)
        if ret == -1 {
                if e1 != 0 {
                        err = error(e1)
                } else {
                        err = syscall.EINVAL
                }
        }
        return
}

func TranslateMessage(msg *Msg) (done bool) {
        r0, _, _ := syscall.Syscall(procTranslateMessage.Addr(), 1, uintptr(unsafe.Pointer(msg)), 0, 0)
        done = bool(r0 != 0)
        return
}

func DispatchMessage(msg *Msg) (ret int32) {
        r0, _, _ := syscall.Syscall(procDispatchMessageW.Addr(), 1, uintptr(unsafe.Pointer(msg)), 0, 0)
        ret = int32(r0)
        return
}

func LoadIcon(instance syscall.Handle, iconname *uint16) (icon syscall.Handle, err error) {
        r0, _, e1 := syscall.Syscall(procLoadIconW.Addr(), 2, uintptr(instance), uintptr(unsafe.Pointer(iconname)), 0)
        icon = syscall.Handle(r0)
        if icon == 0 {
                if e1 != 0 {
                        err = error(e1)
                } else {
                        err = syscall.EINVAL
                }
        }
        return
}

func LoadCursor(instance syscall.Handle, cursorname *uint16) (cursor syscall.Handle, err error) {
        r0, _, e1 := syscall.Syscall(procLoadCursorW.Addr(), 2, uintptr(instance), uintptr(unsafe.Pointer(cursorname)), 0)
        cursor = syscall.Handle(r0)
        if cursor == 0 {
                if e1 != 0 {
                        err = error(e1)
                } else {
                        err = syscall.EINVAL
                }
        }
        return
}

func SetCursor(cursor syscall.Handle) (precursor syscall.Handle, err error) {
        r0, _, e1 := syscall.Syscall(procSetCursor.Addr(), 1, uintptr(cursor), 0, 0)
        precursor = syscall.Handle(r0)
        if precursor == 0 {
                if e1 != 0 {
                        err = error(e1)
                } else {
                        err = syscall.EINVAL
                }
        }
        return
}

func SendMessage(hwnd syscall.Handle, msg uint32, wparam uintptr, lparam uintptr) (lresult uintptr) {
        r0, _, _ := syscall.Syscall6(procSendMessageW.Addr(), 4, uintptr(hwnd), uintptr(msg), uintptr(wparam), uintptr(lparam), 0, 0)
        lresult = uintptr(r0)
        return
}

func PostMessage(hwnd syscall.Handle, msg uint32, wparam uintptr, lparam uintptr) (err error) {
        r1, _, e1 := syscall.Syscall6(procPostMessageW.Addr(), 4, uintptr(hwnd), uintptr(msg), uintptr(wparam), uintptr(lparam), 0, 0)
        if int(r1) == 0 {
                if e1 != 0 {
                        err = error(e1)
                } else {
                        err = syscall.EINVAL
                }
        }
        return
}

// some help functions

func abortf(format string, a ...interface{}) {
        fmt.Fprintf(os.Stdout, format, a...)
        os.Exit(1)
}

func abortErrNo(funcname string, err error) {
        errno, _ := err.(syscall.Errno)
        abortf("%s failed: %d %s\n", funcname, uint32(errno), err)
}

// global vars

var (
        mh syscall.Handle
        bh syscall.Handle
)

// WinProc called by windows to notify us of all windows events we might be interested in.
func WndProc(hwnd syscall.Handle, msg uint32, wparam, lparam uintptr) (rc uintptr) {
        switch msg {
        case WM_CREATE:
                var e error
                // CreateWindowEx
                bh, e = CreateWindowEx(
                        0,
                        syscall.StringToUTF16Ptr("button"),
                        syscall.StringToUTF16Ptr("Quit"),
                        WS_CHILD|WS_VISIBLE|BS_DEFPUSHBUTTON,
                        75, 70, 140, 25,
                        hwnd, 1, mh, 0)
                if e != nil {
                        abortErrNo("CreateWindowEx", e)
                }
                fmt.Printf("button handle is %x\n", bh)
                rc = DefWindowProc(hwnd, msg, wparam, lparam)
        case WM_COMMAND:
                switch syscall.Handle(lparam) {
                case bh:
                        e := PostMessage(hwnd, WM_CLOSE, 0, 0)
                        if e != nil {
                                abortErrNo("PostMessage", e)
                        }
                default:
                        rc = DefWindowProc(hwnd, msg, wparam, lparam)
                }
        case WM_CLOSE:
                DestroyWindow(hwnd)
        case WM_DESTROY:
                PostQuitMessage(0)
        default:
                rc = DefWindowProc(hwnd, msg, wparam, lparam)
        }
        //fmt.Printf("WndProc(0x%08x, %d, 0x%08x, 0x%08x) (%d)\n", hwnd, msg, wparam, lparam, rc)
        return
}

func rungui() int {
        var e error

        // GetModuleHandle
        mh, e = GetModuleHandle(nil)
        if e != nil {
                abortErrNo("GetModuleHandle", e)
        }

        // Get icon we're going to use.
        myicon, e := LoadIcon(0, IDI_APPLICATION)
        if e != nil {
                abortErrNo("LoadIcon", e)
        }

        // Get cursor we're going to use.
        mycursor, e := LoadCursor(0, IDC_ARROW)
        if e != nil {
                abortErrNo("LoadCursor", e)
        }

        // Create callback
        wproc := syscall.NewCallback(WndProc)

        // RegisterClassEx
        wcname := syscall.StringToUTF16Ptr("myWindowClass")
        var wc Wndclassex
        wc.Size = uint32(unsafe.Sizeof(wc))
        wc.WndProc = wproc
        wc.Instance = mh
        wc.Icon = myicon
        wc.Cursor = mycursor
        wc.Background = COLOR_BTNFACE + 1
        wc.MenuName = nil
        wc.ClassName = wcname
        wc.IconSm = myicon
        if _, e := RegisterClassEx(&wc); e != nil {
                abortErrNo("RegisterClassEx", e)
        }

        // CreateWindowEx
        wh, e := CreateWindowEx(
                WS_EX_CLIENTEDGE,
                wcname,
                syscall.StringToUTF16Ptr("My window"),
                WS_OVERLAPPEDWINDOW,
                CW_USEDEFAULT, CW_USEDEFAULT, 300, 200,
                0, 0, mh, 0)
        if e != nil {
                abortErrNo("CreateWindowEx", e)
        }
        fmt.Printf("main window handle is %x\n", wh)

        // ShowWindow
        ShowWindow(wh, SW_SHOWDEFAULT)

        // UpdateWindow
        if e := UpdateWindow(wh); e != nil {
                abortErrNo("UpdateWindow", e)
        }

        // Process all windows messages until WM_QUIT.
        var m Msg
        for {
                r, e := GetMessage(&m, 0, 0, 0)
                if e != nil {
                        abortErrNo("GetMessage", e)
                }
                if r == 0 {
                        // WM_QUIT received -> get out
                        break
                }
                TranslateMessage(&m)
                DispatchMessage(&m)
        }
        return int(m.Wparam)
}

func main() {
        rc := rungui()
        os.Exit(rc)
}
    


x = M.
A yes... 1.5 MB!!!!!!!!!
not kb.
Post 12 Aug 2012, 05:02
View user's profile Send private message Reply with quote
baldr



Joined: 19 Mar 2008
Posts: 1651
baldr
It seems that discussion flows sideways. I have only one question at hand: only the wide spread saves the JavaScript from the oblivion? Is it "language of the 'Net" thingy? (Heh, that's two-in-a-one indeed ;-)
Post 15 Aug 2012, 21:02
View user's profile Send private message Reply with quote
Enko



Joined: 03 Apr 2007
Posts: 678
Location: Mar del Plata
Enko
The cpu nowdays are really powerfull, so actually there is no much room for optimization. Metro in windows uses html5 and javascript...
Post 15 Aug 2012, 21:12
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17278
Location: In your JS exploiting you and your system
revolution
Enko wrote:
The cpu nowdays are really powerfull, so actually there is no much room for optimization.
Hmm, well I'm not so sure about that. The more that CPUs can do the more people want to make them do. I don't ever foresee a time when CPUs will be powerful enough that optimisation is neither needed or desired.
Post 15 Aug 2012, 21:16
View user's profile Send private message Visit poster's website Reply with quote
Enko



Joined: 03 Apr 2007
Posts: 678
Location: Mar del Plata
Enko
Sorry, for the missunderstanding.

What I really meant was:
"there is no room" for optimization.
(a little bit of sarcasm)

The programers are really lazy. In the place of making things better its prefered to make them quicker.

Windows 8 will run on ARM tablets. Even if its dual core ARM 1GHZ a big javascript code won´t work smoothly.
Post 15 Aug 2012, 21:23
View user's profile Send private message Reply with quote
baldr



Joined: 19 Mar 2008
Posts: 1651
baldr
Enko,

Power of the execution unit has nothing to do with expressiveness of the language you've to talk with it. Lisp is expressive (unless you hate parens ;-), Prolog is expressive, many of them are. So why do we stick with this "agreed upon" mess? (Camel is a horse designed by committee ;-)
Post 15 Aug 2012, 21:23
View user's profile Send private message Reply with quote
Enko



Joined: 03 Apr 2007
Posts: 678
Location: Mar del Plata
Enko
IMHO My guess would be that there is lots of programmers that already use it. And an experienced programmer would be some how stubborn to learn new language.
Post 15 Aug 2012, 21:28
View user's profile Send private message Reply with quote
baldr



Joined: 19 Mar 2008
Posts: 1651
baldr
Enko,

"Old dog and new tricks", yeah? Those who can't (or won't) learn from other sources are doomed to copy-paste (even if it was from their brains ;-)

Call by name (of Algol's fame) is quite dirty but useful trick, I miss it so much. It's so lambda... ;-)
Post 15 Aug 2012, 21:37
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You can attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar.

Powered by rwasa.